使用CGImageSourceCreateThumbnailAtIndex从UIImage创建缩略图

时间:2016-11-18 11:11:41

标签: ios uiimage cgimageref cgimagesource

我想使用函数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,该图像在内存上,而不在磁盘上,我不想要将它保存到磁盘,或性能将耗尽。

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

3 个答案:

答案 0 :(得分:15)

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)

答案 1 :(得分:8)

您是否尝试使用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图片,则可以使用CGImageSourceRef创建CGImageSourceCreateWithURL

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

答案 2 :(得分:3)

  

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)

  }
}