正确的进口方式是什么;从iPhone相册中保存照片?
我正在将照片从iPhone相册导入应用程序的documents文件夹。这是我的密码正确的进口方式是什么;从iPhone相册中保存照片?,iphone,objective-c,ios,alassetslibrary,writetofile,Iphone,Objective C,Ios,Alassetslibrary,Writetofile,我正在将照片从iPhone相册导入应用程序的documents文件夹。这是我的密码 for (int j=0; j<[assetArray count]; j++) { ALAsset *assest = [assetArray objectAtIndex:j]; CGImageRef imageRef = assest.defaultRepresentation.fullResolutionImage; UIImage *image = [UIImage ima
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];
}
for(int j=0;j我使用的是ELCImagePicker,在使用asselts从照片库一次导入多张照片时遇到了相同的问题。我们无法减少导入所需的时间,但崩溃问题将得到解决
for (int j=0; j<[assetArray count]; j++)
{
@autoreleasepool // This is compiler level feature so will only work on xcode 4.1 or above
{
ALAsset *assest = [assetArray objectAtIndex:j];
CGImageRef imageRef = assest.defaultRepresentation.fullResolutionImage;
UIImage *image = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:documentsPath atomically:YES];
}
}
出于以下几个原因,将fullResolutionImage用于此任务是危险的。
一些评论:
对于大型图像,可能会出现内存问题。使用fullResolutionImage方法时。请注意,照片库(至少在iPad上)也可能包含原始图像
性能不太理想,因为从imagefile ImageIO内部首先创建一个CGImageRef,然后将其转换为JPEG。这需要时间
资产库还可以包含视频。在这种情况下,fullResolutionImage只返回视频的预览图像,而不返回实际视频
存储实际的资产对象没有问题,因为它们在内存中很小
将图像写入documents目录的更好方法是使用AlassetRepresentation的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];
}
}
//读取原始图像
对于(int j=0;jWe可以缩短时间。我看到这个应用程序导入速度非常快。在设备而不是模拟器中导入照片需要多少时间?请添加更多细节,如照片数量分辨率?autoreleasepool修复了崩溃…谢谢..但我不知道如何缩短时间…我已经测试了导入时间taken用上面的代码导入1.7 MB大小的文件大约需要2到2.3秒。对于大小不同的2个文件(1.7 MB和0.8 MB),最长4秒也有同样的问题,但我通过将资源URL保存在数据库中解决了这个问题。当我需要在documents目录中存储图像时,我会逐个获取资源并将图像存储在documents目录中。一个原因可能是您正在将资产保存在数组save asset url中,还有一件事是在自动释放池中运行此for循环,这将减少内存问题。谢谢您,它工作得非常好。但是我如何将缩略图和全屏图像复制到documents文件夹?或者是否有其他方法可以从集合中获取这些图像S文档文件夹中的演示文稿?我修改了源代码,包括读取全屏图像和缩略图。对于这两幅图像,您可以使用以前的方法,因为它们的大小有限,与fullResolutionImage相比,只消耗可预测的内存量。根据您想做的事情,您可能会考虑呃,将全屏图像保存为JPEG而不是PNG,但这只是一个小的代码更改。谢谢。我们的应用程序一直崩溃,但现在没有。documentPath在这里是什么
//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];
}
}