【问题标题】:Get the actual NSData from ALAsset (Everytime I get bigger image size)从 ALAsset 获取实际的 NSData(每次我得到更大的图像尺寸)
【发布时间】:2014-02-20 02:15:14
【问题描述】:

我要做的是从 ALAsset(图像)获取 NSData,磁盘上图像的大小是 75 KB,但是当我试图从该资产中获取 NSData 时,它返回的 NSData 大小非常大,我的问题是“如何从图像中返回 75KB 的数据而不是更大的尺寸”

我的代码:

ALAssetRepresentation* representation = [asset defaultRepresentation];
CGFloat scale  = 1;
UIImage* image = [UIImage imageWithCGImage:[representation fullResolutionImage]
                                             scale:scale orientation:orientation];
NSData *data = UIImagePNGRepresentation(image);

【问题讨论】:

  • 也许您想要 JPEG 表示而不是 PNG。
  • 你是怎么得到这 75KB 的?

标签: ios iphone objective-c


【解决方案1】:

iOS 9 更新:在 iOS 9 及更高版本中,您应该在 PHImageManager 上使用 requestImageDataForAsset:options:resultHandler:,因为以下代码使用了已弃用的方法。


您是否查看了类参考中的“Getting Raw Data”部分?

ALAssetRepresentation *representation = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);

// add error checking here
NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *sourceData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

【讨论】:

  • 感谢亚伦的帮助
  • 此方法的警告;这确实是一种很好的记忆方法。但是如果你使用这种方法,图像会失去方向。 (每张图片都是横向的,不管它们的方向)。我正在寻找一种方法,但到目前为止还没有结果
猜你喜欢
  • 2013-06-10
  • 2019-04-27
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多