本文介绍了在不重新压缩的情况下保存到用户库中/从中获取JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来读取和写入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.

推荐答案

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.

相反,你应该使用,类似这样:

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 您现在可以获得使用方法:

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.

这篇关于在不重新压缩的情况下保存到用户库中/从中获取JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:46