How to save and reload image using ALAsset?

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

+4
source share
1 answer

A simple solution is that you pass in absoluteString instead of NSUrl.

This code works for me when inside the collector ends.

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera ) { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error ) { NSLog(@"IMAGE SAVED TO PHOTO ALBUM"); [library assetForURL:assetURL resultBlock:^(ALAsset *asset ) { NSLog(@"we have our ALAsset!"); } failureBlock:^(NSError *error ) { NSLog(@"Error loading asset"); }]; }]; } 
+7
source

Source: https://habr.com/ru/post/1395559/


All Articles