gpt4 book ai didi

ios5 - 在 iOS5 中保存通过 UIImagePickerController 捕获的图像后清零 ALAssetRepresentation

转载 作者:行者123 更新时间:2023-12-04 05:53:06 24 4
gpt4 key购买 nike

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

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

我初始化 ALAssetsLibrary AppDelegate , 所以
“从库实例中获取的对象的生命周期与库实例的生命周期相关。”条件还可以。

有没有办法防止 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];
}
}];
}

最佳答案

我解决了!

ALAssetsLibraryChangedNotification Sent when the contents of the assets library have changed from under the app that is using the data. When you receive this notification, you should discard any cached information and query the assets library again. You should consider invalid any ALAsset, ALAssetsGroup, or ALAssetRepresentation objects you are referencing after finishing processing the notification.

关于ios5 - 在 iOS5 中保存通过 UIImagePickerController 捕获的图像后清零 ALAssetRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9836061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com