簡體   English   中英

使用 CGImageSourceCreateThumbnailAtIndex 從 UIImage 創建縮略圖

[英]Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex

我想使用 function 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);

The first parameter of this function demands a CGImageSourceRef , but like I said, I just have an UIImage , that image is on memory, not on disk, and I don't want to save it to disk, or the performance will go down the流走。

如何從UIImage上的 UIImage 獲取CGImageSourceRef ???

斯威夫特 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)

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)

  }
}

您是否嘗試使用CGImageSourceCreateWithData並將圖像數據作為CFDataRef傳遞,如下所示:

注意:使用CGImageSourceCreateWithData會丟失有關圖像旋轉的信息,使用CGImageSourceCreateWithURL可以輕松旋轉最終圖像。

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);
CFRelease(src);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;

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

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

Swift 和iOS 15

func preparingThumbnail(of size: CGSize) -> UIImage?

示例是:

let image = UIImage(named: "info")
image.preparingThumbnail(of size: CFSize(width: 300, height: 400))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM