保存通过iOS5中的UIImagePickerController捕获的图像后归零ALAssetRepresentation

时间:2012-03-23 08:32:51

标签: ios5 uiimagepickercontroller alassetslibrary

在我的应用程序(在iOS 4下工作)中,我收集通过 UIImagePickerController 选择的图片。不幸的是,升级到iOS 5后我遇到了一个奇怪的问题。

简而言之,我将 ALAssetRepresentation 存储在 NSMutableArray 中。当我从图书馆添加照片时,一切正常。但是,当我捕获并保存图片时,所有 ALAssetRepresentations (包括一个新的)都变为0大小。 ALAssetRepresentation.size ALAssetRepresentation.getBytes:fromOffset:length:错误:返回0且 getBytes:错误为零。

我在 AppDelegate 中初始化 ALAssetsLibrary ,所以 “从库实例返回的对象的生命周期与库实例的生命周期相关联。”条件正常。

有没有办法阻止ALAssetRepresentation归零?或者我怎么能在这之后按字节读取图像?

我的代码:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary){
    [self addPhoto:[info valueForKey:UIImagePickerControllerReferenceURL]];
}
else if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera){
    [self savePhoto:[info valueForKey:UIImagePickerControllerOriginalImage]];
}

[self dismissModalViewControllerAnimated:YES];
}

-(ALAssetsLibrary*) getLibrary{
if (!library){
    testAppDelegate *appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
    library = appDelegate.library;
}

NSLog(@"getLibrary: %@", library);
return library;
}

-(void) addPhoto:(NSURL*) url{
ALAssetsLibraryAssetForURLResultBlock successBlock = ^(ALAsset *asset_){
   ALAssetRepresentation *assetRepresentation = [[asset_ defaultRepresentation] retain];
   [photos addObject: assetRepresentation];
};

ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){
    NSLog(@"Error: Cannot get image. %@", [error localizedDescription]);
};

 [[self getLibrary] assetForURL:url resultBlock:successBlock failureBlock:failureBlock];
}

- (void)savePhoto:(UIImage *)image {

[[self getLibrary] writeImageToSavedPhotosAlbum:[image CGImage] orientation:[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error) {
    if (error) {
        NSLog(@"Error: Cannot save image. %@", [error localizedDescription]);
    } else {
        NSLog(@"photo saved");
        [self addPhoto:assetURL];
    }
}];
}

2 个答案:

答案 0 :(得分:3)

我解决了!

  

<强> ALAssetsLibraryChangedNotification   当资产库的内容已从使用该数据的应用程序下更改时发送。   收到此通知后,您应丢弃所有缓存的信息并再次查询资产库。在完成处理通知后,您应该考虑将要引用的任何ALAsset,ALAssetsGroup或ALAssetRepresentation对象无效。

答案 1 :(得分:0)

您是否尝试过保留ALAssets而不是ALAssetRepresentation?这应该有效,我之前使用过这种方法。