将照片移动到其他文件夹ios

时间:2014-08-27 09:14:44

标签: ios objective-c iphone

如何将照片从一个文件夹移动到iphone中的另一个文件夹?

我从相机胶卷中取出了所有照片。现在,我希望他们中的一些移动到iPhone中的某个特定文件夹。有可能吗?

4 个答案:

答案 0 :(得分:2)

是的你可以,有点儿。您可以访问用户媒体库,然后将其复制到应用程序沙箱环境中。由于iOS没有公共文件系统,因此无法将它们直接复制/移动到某个目录。

可以使用AssetsLibrary访问用户媒体库中的图像。

您可以将新组添加到用户媒体库并将图片添加到该组。但您无法移动删除替换用户媒体库中的现有图片。

答案 1 :(得分:0)

试试这个:

NSFileManager *filemgr;

filemgr = [NSFileManager defaultManager];

NSURL *oldDir = [NSURL fileURLWithPath:@"/tmp/mynewdir"];
NSURL *newDir = [NSURL fileURLWithPath:@"/tmp/mynewdir2"];

[filemgr moveItemAtURL: oldDir toURL: newDir error: nil];

希望这会有所帮助.. :)

答案 2 :(得分:0)

  1. 您可以将所有文件移动到文档下文件夹或缓存文件夹。

  2. 您可以在iPhone照片中以编程方式创建文件夹。

  3. 请查看以下链接以供参考:

    iphone: copy or move file from document directory folder

答案 3 :(得分:0)

这里有一些(未经测试的)代码,用于复制缩略图和从共享相机胶卷到应用程序沙箱中文档目录的名称为“名称”的完整尺寸版本的照片:

        NSString *newNameThumb = @"newThumbName.png";
        NSString *newName = @"newFullsizeName.png";
        NSString *name = @"nameOfSharedPhotoToMove.png";

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
        [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

            // Within the group enumeration block, filter to enumerate just photos.
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];

            // Chooses the photo at the last index
            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
                // The end of the enumeration is signaled by asset == nil.
                if (alAsset) {
                    ALAssetRepresentation *representation = [alAsset defaultRepresentation];

                    if([representation.filename isEqualToString:name]){
                        UIImage *latestPhoto =  [UIImage imageWithCGImage:[alAsset thumbnail]];
                        NSData *imageData = UIImagePNGRepresentation(latestPhoto);
                        UIImage *latestFullPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
                        NSData *imageFullData = UIImagePNGRepresentation(latestFullPhoto);

                        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                            NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                            NSString* newThumbPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:newNameThumb];
                            [imageData writeToFile:newThumbPath atomically:YES];

                            NSString* newFullPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:newName];
                            [imageFullData writeToFile:newFullPath atomically:YES];

                            *stop = YES; *innerStop = YES;
                        });
                    }
                }
            }];
        } failureBlock: ^(NSError *error) {
            // Typically you should handle an error more gracefully than this.
            NSLog(@"No photos found or permission denied");
        }];
相关问题