我正在編寫一個應用程序,讓用戶拍照並將其分配給設備。爲此,我需要能夠存儲拍攝圖片的URL並在之後重新加載圖片。如何使用ALAsset保存並重新加載圖像?
我知道已經有上多次討論,但奇怪的是沒有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
我仍然在尋找任何人有一個偉大的想法來幫助我我仍然堅持這個愚蠢的問題。其實這並不難。有大量的應用程序在圖片庫中做些什麼,不在嗎? – sensslen 2012-02-13 07:38:50