一:
ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result && [[result valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypePhoto]) {
ZWPhoto *aPhoto = [[ZWPhoto alloc] initWithAsset:result];
aPhoto.checked = false;
aPhoto.originPhotoNeeded = false;
[self.photosArray addObject:aPhoto];
} else if (self.photosArray.count > 0) {
[self.collectionView reloadData];
}
};
[self.assetsGroup enumerateAssetsUsingBlock:resultsBlock];
二:
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void) {
// 遍历所有相册
[self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// 遍历每个相册中的项ALAsset
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,BOOL *stop) {
__block BOOL foundThePhoto = NO;
if (foundThePhoto){
*stop = YES;
}
// ALAsset的类型
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
if ([assetType isEqualToString:ALAssetTypePhoto]){
foundThePhoto = YES;
*stop = YES;
ALAssetRepresentation *assetRepresentation =[result defaultRepresentation];
CGFloat imageScale = [assetRepresentation scale];
UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(), ^(void) {
CGImageRef imageReference = [assetRepresentation fullResolutionImage];
// 对找到的图片进行操作
UIImage *image =[[UIImage alloc] initWithCGImage:imageReference scale:imageScale orientation:imageOrientation];
_myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
_myImageView.image = image;
[self.view addSubview:_myImageView];
if (image != nil){
//获取到第一张图片
} else {
NSLog(@"Failed to create the image.");
} });
}
}];
}
failureBlock:^(NSError *error) {
NSLog(@"Failed to enumerate the asset groups.");
}];
});