What is the equivalent of @autoreleasepool in Swift?

前端 未结 4 1493
抹茶落季
抹茶落季 2021-02-01 11:35

In Swift, I notice there is no @autoreleasepool{} construct, although Swift does use ARC. What is the proper way to manage an autoreleasepool in Swift, or has it be

4条回答
  •  广开言路
    2021-02-01 12:12

    I used this kind of structure in my code. This function is create thumbnail image from Video URL.

    func getThumbnailImage(forUrl url: URL) -> UIImage? {
        return autoreleasepool{ () -> UIImage in
            let asset: AVAsset = AVAsset(url: url)
            let imageGenerator = AVAssetImageGenerator(asset: asset)
            var thumbnailImage: CGImage?
            do {
                thumbnailImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 1, timescale: 60) , actualTime: nil)
                return UIImage(cgImage: thumbnailImage!)
            } catch let error {
                print(error)
            }
            return UIImage(cgImage: thumbnailImage!)
        }
    }
    

提交回复
热议问题