繁体   English   中英

如何在iOS上从应用程序的文档文件夹中的一个相册中复制所有照片?

[英]How to copy ALL photos from one photo album my app's document folder on iOS?

我想在应用程序启动时自动将iPhone相册中一个相册中的所有照片复制并复制到应用程序的文档文件夹中。

关于使用UIImagePicker的问题很多。 我知道我可以使用图像选择器来做到这一点,并让用户选择照片,但是如果我能在用户不选择照片的情况下做到这一点,我真的很喜欢。

我使用以下问题/答案来帮助以下代码: 如何从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);
}];

如果有人知道或有更好的方法,请分享。 我希望看到它并从中学习。 谢谢!!!

我认为您现有的方法本身可以稍作更改。

删除int num = 1; 在块中并添加__block int num = 1; 可能在assetsLibrary初始化之前。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM