获取所有的cameraroll asynchrously图片 [英] Get All Images from cameraroll asynchrously

查看:566
本文介绍了获取所有的cameraroll asynchrously图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从cameraroll所有图像asynchrously
现在,我使用的code,但我的申请得到了绞刑,直到所有的图像检索,我在instigram或其他应用程序发现它doen't需要时间来从camerroll加载图片。

   - (无效)getAllPictures
{    的NSLog(@我米);    imageArray = [[NSArray的页头]初始化];
    mutableArray = [[NSMutableArray里的alloc]初始化];
    NSMutableArray里* assetURLDictionaries = [[NSMutableArray里的alloc]初始化];
        库= [[ALAssetsLibrary的alloc]初始化];
        无效(^ assetEnumerator)(ALAsset *,NSUInteger,BOOL *)= ^(ALAsset *结果,NSUInteger指数,BOOL *停止){            的NSLog(@我块M);
        如果(结果!=无){
            如果([结果valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]){
                的NSLog(@结果不nill);
                [assetURLDictionaries ADDOBJECT:[导致valueForProperty:ALAssetPropertyURLs]];                NSURL * URL =(NSURL *)[导致defaultRe presentation] URL];
                的NSLog(@URL =%@,URL);
                [库assetForURL:网址
                         resultBlock:^(ALAsset *资产){
                             [mutableArray ADDOBJECT:[UIImage的imageWithCGImage:[[资产defaultRe presentation] fullScreenImage]]];                             imageArray = [[NSArray的页头] initWithArray:mutableArray];
                             [个体经营allPhotosCollected:imageArray];
                             如果([mutableArray计数] ==计数)
                             {的NSLog(@状态%禄%I(无符号长)mutableArray.count,计数);
                                 imageArray = [[NSArray的页头] initWithArray:mutableArray];
                                 [个体经营allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *错误){的NSLog(@操作没有全成!); }];            }
        }
    };
    的NSLog(@图像阵列=%@,imageArray);    NSMutableArray里* assetGroups = [[NSMutableArray里的alloc]初始化];    无效(^ assetGroupEnumerator)(ALAssetsGroup *,* BOOL)= ^(ALAssetsGroup *组,BOOL *停止){
        如果(组!=无){
            的NSLog(@组不nill);
            [组enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups ADDOBJECT:组];
            数=(INT)组numberOfAssets]
        }
    };    assetGroups = [[NSMutableArray里的alloc]初始化];    [库enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *错误){的NSLog(@这是一个错误);}];
} - (无效)allPhotosCollected:(NSArray的*)imgArray
{
    //从库中获得所有的照片后写在这里您code ...
    的NSLog(@的所有图片都是%@,imgArray);
    [_collection_view reloadData];   / *
    的CGRect collectionFrame = self.collection_view.frame;    collectionFrame.size.height = 700;
    collectionFrame.size.width = 320; * /self.verticalLayoutConstraint.constant = 700;
    //self.collection_view.frame=collectionFrame;
}


解决方案

尝试这样的。它的增长速度快。如果你将获得阵列中的所有图像,然后它会在使用iPhone和放大器崩溃您的应用程序; iPad的包含1000多张照片。因此,尝试这样的。

 库= [[ALAssetsLibrary的alloc]初始化];如果(库==无)
    库= [[ALAssetsLibrary的alloc]初始化];[库enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *组,BOOL *停止)
{    [组enumerateAssetsUsingBlock:^(ALAsset *资产,NSUInteger指数,BOOL *停止)
     {
         如果(资产)
         {
             [imageArray ADDOBJECT:资产];
             [collectionViewObj reloadData];
         }
     }];
 }
                failureBlock:^(NSError *误差)
{
    UIAlertView中* alertViewObj = [[UIAlertView中页头] initWithTitle:@Max6Miz无法访问您的照片消息:@你可以在设置中启用获得委托:自我cancelButtonTitle:无otherButtonTitles:@设置,@确定 ,零]
    [alertViewObj秀]
    的NSLog(@错误);
}];

使用显示在你这样的委托函数图像此的CollectionView cellForItemAtIndexPath

  ALAsset *资产=(ALAsset *)imageArray [indexPath.row]
*的UIImage = showingImage [UIImage的imageWithCGImage:资产缩略图]];

I want to get all images from cameraroll asynchrously Now I am using that code but my application got hanged untill all the images retrieved , I noticed in instigram or other application it doen't take time to load picture from camerroll.

-(void)getAllPictures
{

    NSLog(@"i m in ");

    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
        library = [[ALAssetsLibrary alloc] init];
        void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

            NSLog(@"i m in block");
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {


                NSLog(@"result not nill");


                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url];
                NSLog(@"url=%@",url);
                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];





                             imageArray=[[NSArray alloc] initWithArray:mutableArray];
                             [self allPhotosCollected:imageArray];


                             if ([mutableArray count]==count)
                             {NSLog(@"condition %lu , %i",(unsigned long)mutableArray.count,count);


                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];

            }
        }
    };


    NSLog(@"image array= %@",imageArray);

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {


            NSLog(@"group not nill");
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=(int)[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}

-(void)allPhotosCollected:(NSArray*)imgArray
{
    //write your code here after getting all the photos from library...
    NSLog(@"all pictures are %@",imgArray);
    [_collection_view reloadData];



   /*
    CGRect collectionFrame = self.collection_view.frame;

    collectionFrame.size.height=700;
    collectionFrame.size.width=320;*/

self.verticalLayoutConstraint.constant = 700;
    //self.collection_view.frame=collectionFrame;
}

解决方案

Try like this .. Its increasing fast speed. If you will get all images in array then it will crashing your application while using iPhone & iPad that contain 1000+ photos. So Try like this.

library = [[ALAssetsLibrary alloc] init];

if (library == nil)
    library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{

    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
     {
         if (asset)
         {
             [imageArray addObject:asset];
             [collectionViewObj reloadData];
         }
     }];
 }
                failureBlock:^(NSError *error) 
{
    UIAlertView *alertViewObj = [[UIAlertView alloc]initWithTitle:@"Max6Miz does not have access to your photos" message:@"You can enable access in Settings" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Settings",@"Ok", nil];
    [alertViewObj show];
    NSLog(@"error");
}];

Show your image in your delegate function like this using this collectionView cellForItemAtIndexPath

ALAsset *asset = (ALAsset *)imageArray[indexPath.row];
UIImage *showingImage = [UIImage imageWithCGImage:[asset thumbnail]];

这篇关于获取所有的cameraroll asynchrously图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆