1

According to the documentation, the return value of CGImageSourceCreateWithData(::) is

An image source. You are responsible for releasing this object using CFRelease.

That is to say I need to invoke CFRelease in my codes.

func downsampling(imageName: String, to pointSize: CGSize, scale: CGFloat = UIScreen.main.scale) -> UIImage? {
    let imageData = NSDataAsset(name: imageName)?.data
    let imageSourceOptions = [kCGImageSourceShouldCache: false]
    guard let imageSource = CGImageSourceCreateWithData(imageData! as CFData, imageSourceOptions as CFDictionary) else {
        return nil
    }
    let maxDimensionInPixels = min(pointSize.width, pointSize.height/4) * scale
    let downsamplingOptions = [
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceShouldCacheImmediately: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels
    ] as CFDictionary
    guard let downsampledImage = CGImageSourceCreateImageAtIndex(imageSource, 0, downsamplingOptions) else {
        return nil
    }
    **CFRelease(imageSource)**
    return UIImage(cgImage: downsampledImage)
}

However, the compiler gives back an error

'CFRelease' is unavailable: Core Foundation objects are automatically memory managed"

Is CFRelease only suitable for oc? Is it a clerical error in the doc for swift?

Kins
  • 547
  • 1
  • 5
  • 22
test1229
  • 125
  • 9

0 Answers0