Slide 30
Slide 30 text
func downloadWidgetImage(
with url: URL?,
size: ImageSize? = nil,
context: TimelineProviderContextProtocol
) async -> UIImage? {
guard let url else { return await downloadImage(with: url, size: size) }
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, imageSourceOptions) else { return nil }
let displayScale = context.displayScale ?? 1
let maxDimensionInPixels = min(context.displaySize.width, context.displaySize.height) * displayScale * 0.7
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels
] as CFDictionary
guard let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions) else { return nil }
return UIImage(cgImage: downsampledImage)
}
技術的知見とプラクティス
30