I am writing an application that allows users to take photos and assign them to a device. To do this, I need to be able to save the URL of the captured image and reload the image.
I know there have been several discussions about this, but strangely not one of Ansers has helped me ...
So here is my code for saving the image:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; UIImage * image; // Request to save Image if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) { image = [info objectForKey:UIImagePickerControllerEditedImage]; [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL * assertURL, NSError * error){ if (error) { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"PictureSaveErrorTitle", @"Title if saving picture fails") message:NSLocalizedString(@"PictureSaveErrorMessage", @"Message if saving picture fails") delegate:nil cancelButtonTitle:NSLocalizedString(@"PictureSaveErrorOK", @"OK if saving picture fails") otherButtonTitles:nil]; [alert show]; } else { [self setUserImage:[assertURL absoluteString]]; [imageOfDevice setImage:image]; } }]; } else { image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self setUserImage:[[info valueForKey:UIImagePickerControllerReferenceURL] path]]; } [self dismissModalViewControllerAnimated:YES]; }
And this is for reloading the image:
if (userImage != nil) { ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; [library assetForURL:[NSURL fileURLWithPath:userImage] resultBlock:^(ALAsset * asset){ [imageOfDevice setImage:[UIImage imageWithCGImage:[asset thumbnail]]]; } failureBlock:^(NSError * error){ NSLog(@"cannot get image - %@", [error localizedDescription]); }]; }
Anyone see any mistake?
Thanks for the help,
sensslen
source share