// 원하는 해상도에 맞게 조절 func downSample1(scale: CGFloat) -> UIImage { let imageSourceOption = [kCGImageSourceShouldCache: false] as CFDictionary let data = self.pngData()! as CFData let imageSource = CGImageSourceCreateWithData(data, nil)! let maxPixel = max(self.size.width, self.size.height) * scale let downSampleOptions = [ kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceShouldCacheImmediately: true, kCGImageSourceCreateThumbnailWithTransform: true, kCGImageSourceThumbnailMaxPixelSize: maxPixel ] as CFDictionary let downSampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downSampleOptions)! let newImage = UIImage(cgImage: downSampledImage) printDataSize(newImage) return newImage } // 이미지뷰 크기에 맞게 조절 func downSample2(size: CGSize, scale: CGFloat = UIScreen.main.scale) -> UIImage { let imageSourceOption = [kCGImageSourceShouldCache: false] as CFDictionary let data = self.pngData()! as CFData let imageSource = CGImageSourceCreateWithData(data, imageSourceOption)! let maxPixel = max(size.width, size.height) * scale let downSampleOptions = [ kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceShouldCacheImmediately: true, kCGImageSourceCreateThumbnailWithTransform: true, kCGImageSourceThumbnailMaxPixelSize: maxPixel ] as CFDictionary let downSampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downSampleOptions)! let newImage = UIImage(cgImage: downSampledImage) printDataSize(newImage) return newImage }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter