Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex

前端 未结 3 582
孤街浪徒
孤街浪徒 2021-02-02 03:48

I want to use the function CGImageSourceCreateThumbnailAtIndex to create a thumbnail from an UIImage. All I have is the UIImage itself. Th

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 04:49

    Swift 4 code

    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)
    

提交回复
热议问题