Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a thumbnail from UIImage in Swift

I would like to take a UIImage and output a 100 by 100 version of it to use as a thumbnail. I found some answers on SO for how to do this in objective-C but not swift and wasn't sure where to start. I also found the link (https://nshipster.com/image-resizing/#technique-3-creating-a-thumbnail-with-image-io) which suggests it isn't as straight forward as I would have hoped. That link had me hopeful that one of the approaches may work, but each references a URL argument which confused me since I am starting with a UIImage as the input.

In a a similar situation (user uploading a picture from phone) I use the code below to create a thumbnail from the asset, I am looking for help doing the same thing when the input is a UIImage instead of a 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
    }
like image 710
Daniel Patriarca Avatar asked Nov 03 '25 08:11

Daniel Patriarca


2 Answers

iOS 15 added the following beta APIs to UIImage.

func prepareThumbnail(of: CGSize, completionHandler: (UIImage?) -> Void)
func preparingThumbnail(of: CGSize) -> UIImage?
func byPreparingThumbnail(ofSize size: CGSize) async -> UIImage?

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

like image 142
Kou Ariga Avatar answered Nov 05 '25 01:11

Kou Ariga


Tested the code, and this works fine for me:- (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
       }
    }
}
like image 22
Lokesh SN Avatar answered Nov 05 '25 01:11

Lokesh SN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!