2015-09-09 32 views
0

我想从摄像头异步获取所有图像 现在我正在使用该代码,但我的应用程序被绞死直到所有的图像检索,我注意到在instigram或其他应用程序,它不需要时间从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; 
} 

回答

1

试试这样..它的增长速度很快。如果您将获得阵列中的所有图像,则会在使用包含1000多张照片的iPhone & iPad时崩溃应用程序。所以试试这个。

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"); 
}]; 

显示您的图片在你这样的委托功能使用这个的CollectionView cellForItemAtIndexPath

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

我们怎样才能得到完整的图像?例如当我们点击缩略图时我想使用orignal图像 – zkn

+0

谢谢我从这里获得全屏图像http://stackoverflow.com/questions/21494338/get-the-image-uiimage-using-asset-url-in-ios -7 – zkn

1

请使用新的现代化Photos framework它可从iOS 8.0。我已经在我自己的iCloud Photo Library上对50k张照片和存储在数百张专辑中的1.5k视频进行了测试。它工作速度快,可以跟踪和动画化专辑和资产的变化。

这是一篇很好的文章开始与Photos frameworkhttps://www.objc.io/issues/21-camera-and-photos/the-photos-framework/

在这种情况下
+0

针对你的反应,但我很喜欢alasset库,我想更喜欢 – zkn