UIImagePickerController Save to Disk then Load to

2019-04-14 05:49发布

I have a UIImagePickerController that saves the image to disk as a png. When I try to load the PNG and set a UIImageView's imageView.image to the file, it is not displaying.

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. Can anyone tell me what I've got wrong here?

Thanks a bunch.

3条回答
神经病院院长
2楼-- · 2019-04-14 06:16

You're writing out an NSData object, not an image. You need to read the NSData object back in and convert to UIImage.

Assuming everything else is correct, try this:

NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];
查看更多
Ridiculous、
3楼-- · 2019-04-14 06:16

Using NSDateFormatterShortStyle will cause the date to be represented as (for example) 12/11/12, which will mess up your file name. 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]];
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-14 06:38

I used this code on one of my test projects and simulated the 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.

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

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. :)

查看更多
登录 后发表回答