简体   繁体   English

UIImagePickerController保存到磁盘然后加载到UIImageView

[英]UIImagePickerController Save to Disk then Load to UIImageView

I have a UIImagePickerController that saves the image to disk as a png. 我有一个UIImagePickerController,它将图像作为png保存到磁盘。 When I try to load the PNG and set a UIImageView's imageView.image to the file, it is not displaying. 当我尝试加载PNG并将UIImageView的imageView.image设置为该文件时,它不会显示。

Here is my code: 这是我的代码:

- (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];
}

Then here is how I try to load it: 那么这是我尝试加载它的方式:

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"];
}

I get the message that it's trying to load the imageView in the debugger, but the image is never displayed in the imageView. 我得到的消息是它试图在调试器中加载imageView,但图像永远不会显示在imageView中。 Can anyone tell me what I've got wrong here? 谁能告诉我这里有什么问题?

Thanks a bunch. 谢谢一堆。

You're writing out an NSData object, not an image. 你正在写出一个NSData对象,而不是一个图像。 You need to read the NSData object back in and convert to UIImage. 您需要重新读取NSData对象并转换为UIImage。

Assuming everything else is correct, try this: 假设其他一切都正确,试试这个:

NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];

Using NSDateFormatterShortStyle will cause the date to be represented as (for example) 12/11/12, which will mess up your file name. 使用NSDateFormatterShortStyle将导致日期表示为(例如)12/11/12,这将弄乱您的文件名。 It will also use a colon in the short time representation, which is illegal. 它也会在短时间内使用冒号,这是非法的。

I would recommend using a more custom date format, such as: 我建议使用更自定义的日期格式,例如:

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]];

I used this code on one of my test projects and simulated the bug. 我在我的一个测试项目中使用了这段代码并模拟了这个bug。 Jordan was right about converting it to NSData first before changing it again to UIImage but the real problem here is on your file naming method. Jordan在将其再次更改为UIImage之前首先将其转换为NSData,但真正的问题在于您的文件命名方法。

I noticed that you have used the current date and time as the file name of your image. 我注意到您已使用当前日期和时间作为图像的文件名。 This includes characters such as ":" and "/" which is not allowed to be used on file names. 这包括诸如“:”和“/”之类的字符,不允许在文件名上使用。 http://www.portfoliofaq.com/pfaq/FAQ00352.htm http://www.portfoliofaq.com/pfaq/FAQ00352.htm

As a solution, I made the date format as what is written below: 作为解决方案,我将日期格式设为如下所示:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMddHHmm"]; NSString *imageName = [NSString stringWithFormat:@"photo-%@.png", [dateFormatter stringFromDate:[NSDate date]]];

Hope this helps others as well. 希望这对其他人也有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM