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



  • 相关阅读:
    Application Loader上传app程序
    Xcode解决代码高亮、语法提示、错误警告等功能失效的解决方法
    c#上iOS apns p12文件制作记录 iOS推送证书制件
    iOS开发~CocoaPods使用详细说明
    ios实现屏幕旋转的方法
    View页面内容的旋转,在某些情况下可替代屏幕旋转使用
    集成乐视点播功能的注意事项
    NSMutableAttributedString 富文本删除线的用法
    Objective-c的@property 详解
    ASIHttpRequest addRequestHeader的处理
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3184836.html
Copyright © 2011-2022 走看看