self.view.backgroundColor = [UIColor whiteColor];
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 = [resultvalueForProperty:ALAssetPropertyType];
if ([assetTypeisEqualToString:ALAssetTypePhoto]){
foundThePhoto = YES;
*stop = YES;
ALAssetRepresentation*assetRepresentation =[result defaultRepresentation];
CGFloat imageScale = [assetRepresentation scale];
UIImageOrientationimageOrientation = (UIImageOrientation)[assetRepresentation orientation];
dispatch_async(dispatch_get_main_queue(), ^(void) {
CGImageRefimageReference = [assetRepresentation fullResolutionImage];
// 对找到的图片进行操作
UIImage *image =[[UIImage alloc] initWithCGImage:imageReference scale:imageScaleorientation:imageOrientation];
if (image != nil){
self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.image = image;
[self.viewaddSubview:self.imageView];
} else {
NSLog(@"Failed to create the image.");
} });
}
}];
}
failureBlock:^(NSError *error) {
NSLog(@"Failed to enumerate the asset groups.");
}];
});