我正在编写一个应用程序,让用户可以拍摄照片并将它们分配给设备。为此,我需要能够存储拍摄图片的 URL,然后重新加载图片。
我知道已经对此进行了多次讨论,但奇怪的是没有一个 Ansers 帮助我......
所以这是我保存图像的代码:
- (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];
}
这是用于重新加载图像:
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]);
}];
}
有没有人看出什么毛病?
感谢您的帮助,
感性