iOS 如何正确的从iPhone相册中导出和保存照片?
2013-10-28 18:18
316 查看
//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]; } }
这是在实际的项目中采取的措施,解决了内存消耗的问题。很实用
相关文章推荐
- iOS如何将照片保存到相册
- iOS下将照片保存到相册的三种方法
- iOS---如何获取手机的本地照片和相册
- iOS开发>学无止境 - 保存照片到系统相册(Photo Album)
- iOS相册中如何删除照片
- ios iphone 如何序列化存储,保存数据。
- iOS之保存图片到系统相册和从系统相册选取一张或者多张照片
- iOS保存系统相册中的照片到应用沙盒中出现的问题--图片旋转90度,使用CGImageRelease造成应用崩溃
- iOS ----------将照片保存到相册
- ios iphone 如何序列化存储,保存数据。
- iOS 将图片保存到iPhone本地相册
- iOS下将照片保存到相册的三种方法
- iOS---如何截图,如何将图片保存到相册
- iOS下将照片保存到相册的三种方法
- iOS开发之保存照片到系统相册(Photo Album)
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- iOS保存App中的照片到系统相册或自建相册的方法
- iOS下将照片保存到相册的三种方法
- iOS---如何截图,如何将图片保存到相册
- iOS开发之保存照片到系统相册(Photo Album)