• 快速将大图保存到本地


           碰到一个问题:如何快速的批量保存iphone相册中的图片(原始的大图,分辨率高)到本地?

    保存图片到本地,首先得拿到这个图片:alAsset.defaultRepresentation.fullResolutionImage.但是图片太大了,大批量的保存取这个图片就得花很长的时间,再转NSData存入本地又得很久,所以这种读写方式是不行的。

          我google了一下,在stack overflow找到了方法:点击打开链接 。

          我把实现的方法贴一下(包括大图和缩略图):

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



    参考:http://stackoverflow.com/questions/9973259/what-is-the-correct-way-to-import-save-photos-from-iphone-album



  • 相关阅读:
    2020-07-21日报博客
    java第十九天
    Java第十八章
    Java第十七天
    Java第十六天
    Java第十五天
    Java第十四天
    Java第十三天
    Pytorch-数据增强
    Pytorch-实现ResNet-18并在Cifar-10数据集上进行验证
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3184836.html
Copyright © 2020-2023  润新知