gpt4 book ai didi

iphone - 获取照片 alassetlibrary asset representation size zero

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:45 24 4
gpt4 key购买 nike

当我使用 ALAssetLibrary 获取照片时,对于某些图像,AssetRepresentation.size 变为零,这不会在我的 ImageView 上生成图像。这是代码:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:self.groupName]) {

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
//Get the asset type
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]) {
NSLog(@"Photo Asset");
}
//Get URLs for the assets
NSDictionary *assetURLs = [result valueForProperty:ALAssetPropertyURLs];

NSUInteger assetCounter = 0;
for (NSString *assetURLKey in assetURLs) {
assetCounter++;
}
//Get the asset's representation object
ALAssetRepresentation *assetRepresentation = [result defaultRepresentation];

//From this asset representation, we take out data for image and show it using imageview.

dispatch_async(dispatch_get_main_queue(), ^(void){
CGImageRef imgRef = [assetRepresentation fullResolutionImage];
//Img Construction
UIImage *image = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];

NSLog(@"before %@:::%lld", [image description], [assetRepresentation size]); //Prints '0' for size

if((image != nil)&& [assetRepresentation size] != 0){

//display in image view
}
else{
// NSLog(@"Failed to load the image.");
}
});

}];
}
}failureBlock:^(NSError *error){
NSLog(@"Error retrieving photos: %@", error);
}];


[library release];

请帮助。我在这里做错了什么?我应该如何获取图像?

最佳答案

乔·史密斯是对的!你过早地释放图书馆。一旦 AssetLibrary 被释放,所有的 Assets 对象都会随之消失。并且由于这些枚举是 block 代码,因此AssetLibrary的释放将在读取过程中的某个地方执行。

我建议您在应用程序委托(delegate)中创建您的 assetLibrary 以使其保持事件状态,并且仅在您从 ALAssetLibrary 收到更改通知 ALAssetsLibraryChangedNotification 时才重置它

关于iphone - 获取照片 alassetlibrary asset representation size zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772193/

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