【问题标题】:Generate a thumbnail from UIImage in Swift在 Swift 中从 UIImage 生成缩略图
【发布时间】:2019-11-23 19:33:24
【问题描述】:

我想获取一个 UIImage 并输出一个 100 x 100 的版本以用作缩略图。我在 SO 上找到了一些关于如何在 Objective-C 中执行此操作的答案,但不是很快,也不确定从哪里开始。我还找到了链接 (https://nshipster.com/image-resizing/#technique-3-creating-a-thumbnail-with-image-io),这表明它并不像我希望的那样简单。该链接让我希望其中一种方法可能有效,但是每个方法都引用了一个让我感到困惑的 URL 参数,因为我从 UIImage 作为输入开始。

在类似的情况下(用户从手机上传图片),我使用下面的代码从资产创建缩略图,当输入是 UIImage 而不是 PHAsset 时,我正在寻求帮助来做同样的事情。

func getAssetThumbnail(asset: PHAsset) -> UIImage {
        let manager = PHImageManager.default()
        let option = PHImageRequestOptions()
        var thumbnail = UIImage()
        option.isSynchronous = true

        manager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
            thumbnail = result!
        })
        return thumbnail
    }

【问题讨论】:

    标签: swift uiimage thumbnails image-resizing


    【解决方案1】:

    测试了代码,这对我来说很好用:- (Swift 5.0)

    let yourImage = UIImage()
    
    if let imageData = yourImage.pngData(){
        let options = [
            kCGImageSourceCreateThumbnailWithTransform: true,
            kCGImageSourceCreateThumbnailFromImageAlways: true,
            kCGImageSourceThumbnailMaxPixelSize: 100] as CFDictionary // Specify your desired size at kCGImageSourceThumbnailMaxPixelSize. I've specified 100 as per your question
    
        imageData.withUnsafeBytes { ptr in
           guard let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
              return
           }
           if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, imageData.count){
              let source = CGImageSourceCreateWithData(cfData, nil)!
              let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
              let thumbnail = UIImage(cgImage: imageReference) // You get your thumbail here
           }
        }
    }
    

    【讨论】:

      【解决方案2】:

      iOS 15 向 UIImage 添加了以下 beta API。

      func prepareThumbnail(of: CGSize, completionHandler: (UIImage?) -> Void)
      func preparingThumbnail(of: CGSize) -> UIImage?
      

      https://developer.apple.com/documentation/uikit/uiimage/

      【讨论】:

        【解决方案3】:

        为了将来参考,我刚刚遇到了同样的问题,这个帖子有一些很好的解决方案:Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex

        我选择了这个,它在 Swift 5.3 中运行良好:

        let uiImage = someUIImage
        
        let options = [
            kCGImageSourceCreateThumbnailWithTransform: true,
            kCGImageSourceCreateThumbnailFromImageAlways: true,
            kCGImageSourceThumbnailMaxPixelSize: 100] as CFDictionary
        
        guard let imageData = uiImage.pngData(),
              let imageSource = CGImageSourceCreateWithData(imageData as NSData, nil),
              let image = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)
        else {
            return nil
        }
        
        return UIImage(cgImage: image)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-04
          • 2014-01-05
          • 1970-01-01
          • 2014-01-12
          • 2016-05-03
          • 2015-08-24
          • 2010-12-31
          • 2011-04-27
          相关资源
          最近更新 更多