我正在编写一个应用程序,让用户可以拍摄照片并将其分配给设备。为此,我需要能够存储拍摄的图片的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]);
}];
}
有人看到任何错误吗?
感谢您的帮助,
sensslen
答案 0 :(得分:7)
简单的解决方案是您传递的是absoluteString而不是NSUrl。
此代码适用于我内部选择器完成。
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");
}];
}];
}