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



  • 相关阅读:
    springboot + websocket + qpid问题记录
    学习开源项目guns遇到的问题记录
    CSS选择器和jQuery选择器学习总结
    (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
    (深入.Net平台和C#编程)第九章.上机练习.20170417
    (深入.Net平台和C#编程)第八章.上机练习(网络电视精灵).20170415
    (深入.Net平台和C#编程)第十章.课程总复习.20170413
    (深入.Net平台和C#编程)第七章.上机练习.20170412
    (深入.Net平台和C#编程)第六章.简答题5.20170410
    (深入.Net平台和C#编程)第六章.简答题3.20170410
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3184836.html
Copyright © 2011-2022 走看看