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
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!)
}
}
This is explained in detail in WWDC 2014 session video number 418 "Improving Your App with Instruments", which you can also download as a PDF.
But in short, the syntax is:
autoreleasepool {
/* code */
}
There is! It's just not really mentioned anywhere.
autoreleasepool {
Do things....
}
Just FYI, Xcode constructed the full code as follows:
autoreleasepool({ () -> () in
// code
})
Guess the parentheses identifies the functions closure.