2014-03-25 18 views
0

我想访问并将我iPhone相册中的一个相册中的所有照片复制到我的应用程序的文档文件夹AUTOMATICALLY当APP启动时。如何复制iOS上的应用程序的文档文件夹中的所有照片?

这里有很多关于使用UIImagePicker的问题/答案。我知道我可以用图像选择器来做,并让用户选择照片,但如果我可以在没有用户选择照片的情况下做到这一点,我会非常喜欢它。

我已经使用了以下问题/答案,以帮助下面的代码:How to retrieve the most recent photo from Camera Roll on iOS?

我能够成功挽救一个形象在我的文件夹,但无法弄清楚如何节省超过一次一个。

 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; 
[assetsLibrary enumerateGroupsWithTypes: ALAssetsGroupAlbum //ALAssetsGroupAlbum before I CHANGED it 
          usingBlock:^(ALAssetsGroup *group, BOOL *stop) { // 
           if (nil != group) { 
            // be sure to filter the group so you only get photos 
            [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 

            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex: group.numberOfAssets - 1 ]// -1 
                  options:0 
                 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 


                 if (nil != result) 
                   { 
                   ALAssetRepresentation *repr = [result defaultRepresentation]; 
                    // this is the most recent saved photo 
                    UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]]; 
                    // we only need the first (most recent) photo -- stop the enumeration 
                    // *stop = YES; 

                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
                    NSString *documentsDirectory = [paths objectAtIndex:0]; 

                    int num = 1; 
                    NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]]; 
                    num++; 

                    NSData* data = UIImagePNGRepresentation(img); 
                    [data writeToFile:path atomically: YES]; 



                   } 
                  }]; 
           } 

           *stop = NO; 
          } failureBlock:^(NSError *error) { 
           NSLog(@"error: %@", error); 
          }]; 

更新:我能弄清楚,而不是相机胶卷到我的应用程序文件夹使用下面的代码只是一个相册载入图片:

__block int num = 1; 

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; 
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
{ 
    if (nil != group) 
    { 
     [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 

     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){ 

      if (nil != result) 
      { 
       ALAssetRepresentation *repr = [result defaultRepresentation]; 
       // this is the most recent saved photo 
       UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]]; 

       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
       NSString *documentsDirectory = [paths objectAtIndex:0]; 


       NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat:@"%@-%d.png",@"boxOneImage", num]]; 
       num++; 

       NSData* data = UIImagePNGRepresentation(img); 
       [data writeToFile:path atomically: YES]; 

      } 
     }]; 
    } 

    *stop = NO; 
} failureBlock:^(NSError *error) 
{ 
    NSLog(@"error: %@", error); 
}]; 

如果有人或知道一个更好的方式做到这一点,然后请分享。我很乐意看到它并从中学习。谢谢!!!

+0

引用此:http://stackoverflow.com/questions/12633843/get-all-of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr – Mayur

回答

0

我认为你现有的方法本身将工作在一个小的改变。

删除int num = 1;这是在块中加__block int num = 1;以外的块,可能是assetsLibrary之前的初始化。

或者更好的解决方案,完全删除变量num,利用index+1传递给块!

+0

感谢它的工作,但这只允许我使用此代码获取最后两张照片:[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1]。我怎样才能改变这个代码或一般的东西,以获取一张专辑或一组号码的所有图像?我很想看到一些示例代码,如果可能的话......谢谢 –

+0

更新:我用下面的代码:'[组enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,group.numberOfAssets - 1)]'这让我得到所有在我的相机胶卷中的照片,但我想获得我创建的相册中的所有图像。 –

相关问题