Iphone UIImagePickerController保存到磁盘,然后加载到UIImageView
我有一个UIImagePickerController,它将图像作为png保存到磁盘。当我尝试加载PNG并将UIImageView的Iphone UIImagePickerController保存到磁盘,然后加载到UIImageView,iphone,uiimageview,uiimagepickercontroller,Iphone,Uiimageview,Uiimagepickercontroller,我有一个UIImagePickerController,它将图像作为png保存到磁盘。当我尝试加载PNG并将UIImageView的imageView.image设置为文件时,它不会显示 这是我的密码: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info obje
imageView.image
设置为文件时,它不会显示
这是我的密码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(image);
// Create a file name for the image
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *imageName = [NSString stringWithFormat:@"photo-%@.png",
[dateFormatter stringFromDate:[NSDate date]]];
[dateFormatter release];
// Find the path to the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// Write out the data.
[imageData writeToFile:fullPathToFile atomically:NO];
// Set the managedObject's imageLocation attribute and save the managed object context
[self.managedObject setValue:fullPathToFile forKey:@"imageLocation"];
NSError *error = nil;
[[self.managedObject managedObjectContext] save:&error];
[self dismissModalViewControllerAnimated:YES];
}
下面是我尝试加载它的方式:
self.imageView.backgroundColor = [UIColor lightGrayColor];
self.imageView.frame = CGRectMake(10, 10, 72, 72);
if ([self.managedObject valueForKey:@"imageLocation"] != nil) {
NSLog(@"Trying to load the imageView with: %@", [self.managedObject valueForKey:@"imageLocation"]);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
self.imageView.image = image;
} else {
self.imageView.image = [UIImage imageNamed:@"no_picture_taken.png"];
}
我收到一条消息,它正试图在调试器中加载imageView,但图像从未显示在imageView中。有谁能告诉我这里出了什么问题吗
非常感谢。您写的是NSData对象,而不是图像。您需要将NSData对象读回并转换为UIImage 假设其他所有操作都正确,请尝试以下操作:
NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];
使用NSDateFormatterShortStyle将导致日期表示为(例如)12/11/12,这将弄乱文件名。它还将在短时间表示中使用冒号,这是非法的 我建议使用更自定义的日期格式,例如:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"yyyyMMdd-HHmmss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
我在我的一个测试项目中使用了这段代码,并模拟了这个bug。Jordan在将其再次更改为UIImage之前首先将其转换为NSData是正确的,但这里真正的问题在于您的文件命名方法 我注意到您使用了当前日期和时间作为图像的文件名。这包括“:”和“/”等字符,这些字符不允许用于文件名 作为一种解决方案,我制作了如下所示的日期格式:
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyymddhmm”];
NSString*imageName=[NSString stringWithFormat:@“photo-%@.png”,
[dateFormatter stringFromDate:[NSDate date]]代码>
希望这对其他人也有帮助。:) 尝试不保存完整路径,只保存图像名称,并在显示之前查找路径。