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];
}
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
.
It's dangerous to use the fullResolutionImage for this task for several reasons. Some remarks:
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];
}
}