• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在不重新压缩的情况下保存到用户库/从获取JPEG

用户头像
it1352
帮助1

问题说明

我试图找到一种方法来读取和写入JPEG图像到用户图库(相机胶卷),而无需重新压缩iOS。
UIImage似乎是这里的瓶颈。我发现保存到用户库的唯一方法是UIImageWriteToSavedPhotosAlbum()。有没有办法解决这个问题?

I am trying to find a way to read and write JPEG images to user gallery (camera roll) without iOS re-compressing them. UIImage seems to be the bottleneck here. The only method for saving to user gallery I've found is UIImageWriteToSavedPhotosAlbum(). Is there a way around this?

现在我的例程看起来像这样

For now my routine looks like this

-Ask UIImagePickerController的照片。当它确实完成了PickinPickingMediaWithInfo时,执行:

–Ask UIImagePickerController for a photo. And when it didFinishPickingMediaWithInfo, do:

NSData *imgdata = [NSData dataWithData:UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"], 1)];
[imgdata writeToFile:filePath atomically:NO];

- 在磁盘上无损处理JPEG格式。

–Process JPEG losslessly on disk.

- 然后保存回来:

–Then save it back:

UIImageWriteToSavedPhotosAlbum([UIImage imageWithContentsOfFile:[self getImagePath]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

以下是3次传球后质量下降情况的微小动画:

Here is a tiny animation of what quality degradation looks like after 3 passes:

每次我这样做都会变得更糟,但我无法自动化图像拾取部分,以便在50/100/1000周期内对其进行全面测试。

It obviously gets worse each time I do this, but I couldn't automate the image picking part in order to fully test it for 50/100/1000 cycles.

正确答案

#1

UIImage 解码图像数据,以便对其进行编辑和显示,所以

UIImage decodes the image data so it can be edited and displayed, so

UIImageWriteToSavedPhotosAlbum([UIImage imageWithContentsOfFile:[NSData dataWithContentsOfFile:[self getImagePath]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

首先解码图像,然后通过 UIImageWriteToSavedPhotosAlbum 方法。

would first decode the image, and than encode it back by the UIImageWriteToSavedPhotosAlbum method.

相反,你应该使用 ALAssetsLibrary / writeImageDataToSavedPhotosAlbum:metadata:completionBlock:,类似这样:

Instead, you should use ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock:, something like this:

ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLib writeImageDataToSavedPhotosAlbum:[self getImagePath] metadata:nil completionBlock:nil];

您还可以将元数据和完成块传递给通话。

You may also pass metadata and the completion block to the call.

编辑:

获取图片:

[info objectForKey:@UIImagePickerControllerOriginalImage] 包含从 UIImagePickerController中选择的已解码 UIImage / code>。你应该使用

[info objectForKey:@"UIImagePickerControllerOriginalImage"] contains the decoded UIImage selected from UIImagePickerController. You should instead use

NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];

使用 assetURL 您现在可以获得使用 ALAsset #// apple_ref / occ / instm / ALAssetsLibrary / assetForURL%3aresultBlock%3afailureBlock%3a> ALAssetsLibrary / assetForURL:resultBlock:failureBlock:方法:

Using the assetURL you can now get the ALAsset for it using the ALAssetsLibrary/assetForURL:resultBlock:failureBlock: method:

ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLib assetForURL:assetURL resultBlock:resultBlock failureBlock:failureBlock];

您现在可以获得该图像的未更改的NSData:

You can now get the unaltered NSData of that image:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset){
  ALAssetRepresentation *assetRep = [asset defaultRepresentation];
  long long imageDataSize = [assetRepresentation size];
  uint8_t* imageDataBytes = malloc(imageDataSize);
  [assetRepresentation getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
  NSData *imageData = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES]; // you could for instance read data in smaller buffers and append them to your file instead of reading it all at once
  // save it
  [imgdata writeToFile:filePath atomically:NO];
};
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){
  NSLog(@"Cannot get image - %@",[myerror localizedDescription]);
  //
};

我可能在代码中犯了一些错误,但步骤如上所列。如果某些东西不能正常工作,或者你想让它更有效率,那么有很多例子可以从<$ c $中读取 NSData 。 c> stackoverflow或其他网站上的ALAsset 。

I may have made some mistakes in the code, but the steps are as listed above. In case of something not working right, or if you want to make this a little more efficient, there are plenty of examples of doing stuff like reading NSData from an ALAsset on stackoverflow or other sites.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhafb
系列文章
更多 icon
同类精品
更多 icon
继续加载