【问题标题】:Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex使用 CGImageSourceCreateThumbnailAtIndex 从 UIImage 创建缩略图
【发布时间】:2019-09-26 20:10:02
【问题描述】:

我想使用函数CGImageSourceCreateThumbnailAtIndexUIImage 创建缩略图。我所拥有的只是UIImage 本身。图片是UIView 上的快照。

拜托,我不想使用任何其他方法来创建缩略图,只是使用CGImageSourceCreateThumbnailAtIndex 的方法,因为我想将它的性能与我已有的其他方法进行比较。

说,这是我目前的代码。

我使用此代码创建了一个UIImage 类别:

- (UIImage *)createSquaredThumbnailWithWidth:(NSInteger)width {

  CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                         (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                         (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                         (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                         };

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);


  UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef];

  CGImageRelease(scaledImageRef);

  return scaled;
}

我的问题在于这一行:

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);

这个函数的第一个参数需要一个CGImageSourceRef,但就像我说的,我只有一个UIImage,那个图像在内存上,而不是在磁盘上,我不想把它保存到磁盘上,否则性能将付诸东流。

我如何从内存中的UIImage 获取CGImageSourceRef???

【问题讨论】:

  • 内存呢?所有图像实例都在内存中。正如@Nirav D 所说的那样使用它。
  • 出于好奇,从 UIImage 创建缩略图的哪种方法是“最好的”(在内存使用、时间等方面)?

标签: ios uiimage cgimageref cgimagesource


【解决方案1】:

Swift 4 代码

let imageData = UIImagePNGRepresentation(image)!
let options = [
    kCGImageSourceCreateThumbnailWithTransform: true,
    kCGImageSourceCreateThumbnailFromImageAlways: true,
    kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
let source = CGImageSourceCreateWithData(imageData, nil)!
let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
let thumbnail = UIImage(cgImage: imageReference)

【讨论】:

  • 经过一系列的网络搜索,这真的很方便!感谢这个精彩的解决方案。
  • 还可以查看 mattt 的帖子 nshipster.com/image-resizing,其中列出了许多图像缩放技术及其性能基准。
  • 如何使缩略图图像方向不旋转,我尝试将 kCGImageSourceCreateThumbnailWithTransform 更改为 false,但它仍在旋转,我只需要原始图像是纵向还是横向,我不需要缩略图旋转。
【解决方案2】:

Swift 5.1 扩展

基于伊万的回答

extension UIImage {

  func getThumbnail() -> UIImage? {

    guard let imageData = self.pngData() else { return nil }

    let options = [
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary

    guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
    guard let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else { return nil }

    return UIImage(cgImage: imageReference)

  }
}

【讨论】:

  • .jpegData(compressionQuality: 0.95) 比 .pngData() 快,同时提供几乎相同的质量
【解决方案3】:

您是否尝试过像这样使用CGImageSourceCreateWithData 并将图像数据作为CFDataRef 传递。

NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                     (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                     (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                     (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                     };

CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;

注意:如果您有URL 的图像,那么您可以使用CGImageSourceCreateWithURL 创建CGImageSourceRef

CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);

【讨论】:

  • 缺少CFRelease(src)
  • @Pang 如果您面临内存泄漏,请尝试释放 src 对象。
  • 我遇到的内存占用最少的选项之一。很有用,谢谢。
  • 执行 CFRelease(src) 后仍然面临内存泄漏
猜你喜欢
  • 2011-06-18
  • 2015-09-16
  • 2014-09-24
  • 2023-03-25
  • 2015-01-24
  • 2011-12-31
  • 2013-01-21
  • 2015-05-04
  • 2011-02-18
相关资源
最近更新 更多