ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
NSLog(@"%@", group);
[group setAssetsFilter:[ALAssetsFilter allAssets]];
NSLog(@"名称:%@", [group valueForProperty:ALAssetsGroupPropertyName]);
NSNumber* groupType = [group valueForProperty:ALAssetsGroupPropertyType];
switch ([groupType unsignedIntegerValue]) {
case ALAssetsGroupAlbum:
{
NSLog(@"来自我的电脑或者是在设备上创建");
NSString* persistentID = [group valueForProperty:ALAssetsGroupPropertyPersistentID];
if ([[persistentID substringWithRange:NSRangeFromString(@"0,8")] isEqualToString:@"00000000"])
{
NSLog(@"来自我的电脑");
}
break;
}
case ALAssetsGroupSavedPhotos:
NSLog(@"相机胶卷");
break;
case ALAssetsGroupPhotoStream:
NSLog(@"我的照片流");
break;
default:
break;
}
}
failureBlock: ^(NSError *error)
{
NSLog(@"No groups");
}];
//获取相册所有图片并遍历
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];
if (image != nil){
//获取到第一张图片
} else {
NSLog(@"Failed to create the image.");
} });
}
}];
}
failureBlock:^(NSError *error) {
NSLog(@"Failed to enumerate the asset groups.");
}];
});
//保存图片到相册的两种方法
- (void)saveImageToPhotos:(UIImage*)savedImage
{
//第一种保存图片
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
// 第二种保存图片 ALAssetsLibrary
// NSString *imageURL = nil;
// ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
// ALAssetsLibraryWriteImageCompletionBlock completeBlock = ^(NSURL *assetURL, NSError *error){
// if (!error) {
//#pragma mark get image url from camera capture.
// imageURL = [NSString stringWithFormat:@"%@",assetURL];
//
// }
// };
//
// if(savedImage){
//
// [assetsLibrary writeImageToSavedPhotosAlbum:[savedImage CGImage]
// orientation:(ALAssetOrientation)[savedImage imageOrientation]
// completionBlock:completeBlock];
// }
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
if(error){
//保存失败
}else{
//保存成功
}
}