gpt4 book ai didi

iphone - 从 iPhone 相册导入和保存照片的正确方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 06:17:41 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

  1. 对于大图像,可能会出现内存问题。使用 fullResolutionImage 方法时。请注意,照片库(至少在 iPad 上)也可以包含 RAW 图像。
  2. 性能欠佳,因为 ImageIO 从图像文件内部首先创建一个 CGImageRef,然后将其转换为 JPEG。这需要时间。
  3. AssetLibrary 也可以包含视频。在这种情况下,fullResolutionImage 仅返回视频的预览图像,而不是实际视频。
  4. 存储实际的 Assets 对象没有问题,因为它们在内存中很小。

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

重写后的示例代码如下所示:

//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 - 从 iPhone 相册导入和保存照片的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com