zoukankan      html  css  js  c++  java
  • 快速将大图保存到本地

           碰到一个问题:如何快速的批量保存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



  • 相关阅读:
    dedecms代码研究五
    dedecms代码研究四
    判断有没有真正点击打印
    SAP中删除假脱机请求
    商品扩地点不成功
    记录一些使用的abap小程序帮助开发
    sap abap 程序 中使用 FTP . <转载>
    SM37 后台调试
    ftp上传下载| 图片上传下载
    ALV调用的几个标准函数 <转自 思微随想>
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3184836.html
Copyright © 2011-2022 走看看