导入的正确方法是什么?从iPhone相册保存照片? [英] What is the correct way to import & save Photos from iPhone Album?

查看:268
本文介绍了导入的正确方法是什么?从iPhone相册保存照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将照片从iPhone相册导入我的应用程序的文档文件夹。这是我的代码。

I am Importing Photos from the iPhone Album to the documents folder of my application. This is my code.

for (int j=0; j<[assetArray count]; j++) {

    ALAsset *assest = [assetArray objectAtIndex:j];
    CGImageRef imageRef = assest.defaultRepresentation.fullResolutionImage;
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    NSData *imageData = UIImageJPEGRepresentation(image);
    [imageData writeToFile:documentsPath atomically:YES];
}

它工作正常,但当我尝试导入更高分辨率的图像时需要更多时间。以最短时间导入的正确方法是什么?顺便说一下:我将图像转换为 NSData 需要更多时间。

Its working fine but when I try to Import higher resolution Images it takes more time. What is the correct way to Import with least time? BTW: It takes more time when I convert the image into NSData.

推荐答案

出于多种原因,将fullResolutionImage用于此任务是危险的。
一些评论:

It's dangerous to use the fullResolutionImage for this task for several reasons. Some remarks:


  1. 对于大图像,可能会出现内存问题。使用fullResolutionImage方法时。请注意,Photo-Library(至少在iPad上)也可以包含RAW图像。

  2. 从图像文件内部开始,性能不是很理想ImageIO首先创建一个CGImageRef然后转换为JPEG。这需要时间。

  3. AssetLibrary还可以包含视频。在这种情况下,fullResolutionImage只返回视频的previewImage,而不是实际的视频。

  4. 存储实际的资产对象没有问题,因为这些内存很小。

  1. For large images memory problems could occur. when using the fullResolutionImage method. Please note, that the Photo-Library (on the iPad at least) can also contain RAW-images.
  2. The performance is suboptimal as internally from the imagefile ImageIO creates first a CGImageRef that is then converted to a JPEG. This takes time.
  3. The AssetLibrary can also contain videos. In such a case fullResolutionImage only returns a previewImage of the video, but not the actual video.
  4. It's no problem to store the actual Asset-Objects as these are small in memory.

将图像写出到文档目录的更好方法是使用ALAssetsRepresentation的getBytes方法。这应该是更快,更有效的记忆方式。它还为您提供原始图像文件(包括元数据),也适用于视频。

A far better approach to write out the images to the documents directory, would be to use the getBytes method of ALAssetsRepresentation. This should be way faster and more efficient memory wise. It also gives you the original image file (incl. metadata) and also works for videos.

您重写的示例代码将如下所示:

Your example code rewritten then would look like that:

//reading out the orginal images
    for (int j=0; j<[assetArray count]; j++) {

    ALAssetRepresentation *representation = [[assetArray objectAtIndex:j] defaultRepresentation];
    NSString* filename = [documentPath stringByAppendingPathComponent:[representation filename]];

    [[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
    NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
    [outPutStream open];

    long long offset = 0;
    long long bytesRead = 0;

    NSError *error;
    uint8_t * buffer = malloc(131072);
    while (offset<[representation size] && [outPutStream hasSpaceAvailable]) {
        bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
        [outPutStream write:buffer maxLength:bytesRead];
        offset = offset+bytesRead;
    }
    [outPutStream close];
    free(buffer);
}


//reading out the fullScreenImages and thumbnails
for (int j=0; j<[assetArray count]; j++) 
{
    @autoreleasepool
    {

        ALAsset *asset = [assetArray objectAtIndex:j];

         NSString *orgFilename = [representation filename];
         NSString *filenameFullScreen = [NSString stringWithFormat:@"%@_fullscreen.png",[orgFilename stringByDeletingPathExtension]]
         NSString* pathFullScreen = [documentPath stringByAppendingPathComponent:filenameFullScreen];

         CGImageRef imageRefFullScreen = [[asset defaultRepresentation] fullScreenImage];
         UIImage *imageFullScreen = [UIImage imageWithCGImage:imageRefFullScreen];
         NSData *imageDataFullScreen = UIImagePNGRepresentation(imageFullScreen);
         [imageDataFullScreen writeToFile:pathFullScreen atomically:YES];

         NSString *filenameThumb = [NSString stringWithFormat:@"%@_thumb.png",[orgFilename stringByDeletingPathExtension]]
         NSString* pathThumb = [documentPath stringByAppendingPathComponent:filenameThumb];

         CGImageRef imageRefThumb = [asset thumbnail];
         UIImage *imageThumb = [UIImage imageWithCGImage:imageRefThumb];
         NSData *imageDataThumb = UIImagePNGRepresentation(imageThumb);
         [imageDataThumb writeToFile:pathThumb atomically:YES];

    }
}

这篇关于导入的正确方法是什么?从iPhone相册保存照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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