asset需要用到的框架:PhotoKit
需导入:#import <Photos/Photos.h>
OC实现代码:
PHFetchOptions *options = [[PHFetchOptions alloc] init];
//creationDate 获取所有资源的集合,并按资源的创建时间排序
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *set = [PHAsset fetchAssetsWithLocalIdentifiers:@[@"9F983DBA-EC35-42B8-8773-B597CF782EDD"] options:options];
//asset:PHFetchResult图片资源
//targetSize:图片的大小(pixel) ,需要原图的话可设置为:PHImageManagerMaximumSize
//contentMode:图片的裁切方式
//options:nil
//resultHandler:处理后的回调,这里有匹配到的图片资源(result)和图片信息(info)
[[PHImageManager defaultManager] requestImageForAsset:set.firstObject targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage *result, NSDictionary *info){
//result就是id 9F983DBA-EC35-42B8-8773-B597CF782EDD所对应的图片
}];
Swift实现代码:
let options = PHFetchOptions()
options.sortDescriptors = [NSSortDescriptor.init(key: "creationDate", ascending: false)]
let set = PHAsset.fetchAssetsWithLocalIdentifiers(["9F983DBA-EC35-42B8-8773-B597CF782EDD"], options: options)
PHImageManager.defaultManager().requestImageForAsset(set.firstObject as! PHAsset, targetSize: CGSizeZero, contentMode:.AspectFit, options: nil) { (result, info) in
//result就是id 9F983DBA-EC35-42B8-8773-B597CF782EDD所对应的图片
}