本文介绍了ALAssetsLibrary从自定义相册ios获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用ALAssetsLibrary将图像保存在应用程序的海关相册"中.我能够保存图像,但无法从相册"中获取图像
I am using ALAssetsLibrary to save the image in customs Album in app. i am able to save the image but not able to get the image from the Album
__weak ALAssetsLibrary *lib = self.library;
[self.library addAssetsGroupAlbumWithName:@"MyAlbum" resultBlock:^(ALAssetsGroup *group) {
///checks if group previously created
if(group == nil){
//enumerate albums
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *g, BOOL *stop)
{
//if the album is equal to our album
if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {
//save image
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
//then get the image asseturl
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
//put it into our album
[g addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
}failureBlock:^(NSError *error){
}];
}else{
// save image directly to library
[lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
[lib assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
} failureBlock:^(NSError *error) {
}];
}];
}
} failureBlock:^(NSError *error) {
}];
如何从MyAlbum获取图像.我想一张一张地获取所有图像.
How to get image from the MyAlbum . i want to fetch all image one by one.
推荐答案
您可以尝试这种方式.
-(void)loadlibraryImages
{
NSMutableArray * _recentpictureArray=[[NSMutableArray alloc]init];
ALAssetsLibrary *_library = [[ALAssetsLibrary alloc] init];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:@"MyAlbum"]) {
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != nil)
{
[_recentpictureArray addObject:result];
}
};
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
}
[self.collectionview reloadData]; // you can reload your collection view
}
failureBlock:^(NSError *error){
NSLog(@"failure"); }];
NSLog(@"Count : %lu",(unsigned long)[_recentpictureArray count]);
}
然后像这样从Array获得拇指图像.
And After this get thumb image from Array like that.,
ALAsset *asset = [_recentpictureArray objectAtIndex:indexPath.row];
CGImageRef thumbnailImageRef = [asset aspectRatioThumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
// Then set your image view's image.
就是这样.这项工作在ios9上.希望对您有所帮助.谢谢
That's it. This work on ios9..Hope this is helpful..Thanks
这篇关于ALAssetsLibrary从自定义相册ios获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!