标签:
ALAssetsLibrary 提供了访问iOS设备下”照片”应用下所有照片和视频的接口;
其层次关系为 ALAssetsLibrary -> ALAssetsGroup -> ALAsset -> ALAssetRepresentation 。
注意:
The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.
通过ALAssetsLibrary对象获取的其他对象只在该ALAssetsLibrary对象生命期内有效,若ALAssetsLibrary对象被销毁,则其他从它获取的对象将不能被访问,否则有会错误。
invalid attempt to access ALAssetPrivate past the lifetime of its owning ALAssetsLibrary
The method [representation metadata] returns an autoreleased object and possibly creates more autoreleased objects when it executes. All these instances are added to the autorelease pool, waiting to be finally released (and their memory freed) when the ARP gets the chance to drain itself.
[UIImage imageWithCGImage:representation.fullScreenImage];
使用fullResolutionImage要自己调整方法和scale,即
[UIImage imageWithCGImage:representation.fullResolutionImage scale:representation.scale orientation:representation.orientation];
使用ALAssetsLibrary之前需导入头文件和AssetsLibrary.framework。
#import
...
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
...
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (!group) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[groupsArray addObject:group];
...
}
} failureBlock:^(NSError *error) {
NSLog(@"error:%@",error);
}];
[assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (!result) {
[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}else{
[assetsArray addObject:result];
...
}
}];
});
使用ALAssetsLibrary的 assetForURL:resultBlock:failureBlock: 方法,可根据之前从ALAssetRepresentation中获取的url重新获取ALAsset对象,此方法同enumerateGroupsWithTypes:usingBlock:failureBlock:一样为异步方法。
转载自:http://www.tuicool.com/articles/UBZJFb
标签:
原文地址:http://www.cnblogs.com/benbenzhu/p/4437429.html