UIImage에서 GIF 생성
여러 장의 이미지를 GIF로 변환하는 기능이 있어, 조금 조사해 정리했습니다.
아래의 GIF는
String
-> UIImage
-> GIF
로 변환한 샘플입니다.
import UIKit
import ImageIO
import MobileCoreServices
class GIFCreator {
struct GIFFrame {
var image: UIImage
var duration: Float
}
static func create(with frames: [GIFFrame], resizeTo size: CGSize? = nil, filename: String = "temp.gif", completion: @escaping (URL?) -> Void) {
DispatchQueue.global(qos: .userInteractive).async {
// Resize images if needed
let resizedFrames: [GIFFrame]
if let size = size {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
resizedFrames = frames.map { f in
let resizedImage = UIGraphicsImageRenderer(size: size, format: format).image { _ in
f.image.draw(in: CGRect(origin: .zero, size: size))
}
return GIFFrame(image: resizedImage, duration: f.duration)
}
} else {
resizedFrames = frames
}
let cacheUrl = try! FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = cacheUrl.appendingPathComponent(filename)
try? FileManager.default.removeItem(at: url)
let cfURL = url as CFURL
if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, resizedFrames.count, nil) {
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
for frame in resizedFrames {
let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frame.duration]]
CGImageDestinationAddImage(destination, frame.image.cgImage!, gifProperties as CFDictionary?)
}
DispatchQueue.main.async {
CGImageDestinationFinalize(destination) ? completion(url) : completion(nil)
}
} else {
DispatchQueue.main.async {
completion(nil)
}
}
}
}
static func create(with images: [UIImage], perFrameDuration: Float = 0.1, resizeTo size: CGSize? = nil, filename: String = "temp.gif", completion: @escaping (URL?) -> Void) {
GIFCreator.create(with: images.map { GIFFrame(image: $0, duration: perFrameDuration)}, resizeTo: size, filename: filename, completion: completion)
}
}
사용법은
1, 각 화상의 표시 시간이 고정의 경우는
[UIImage]
를 GIFCreator
에 건네주면 OK입니다.
let images = ...
GIFCreator.create(with: images) { url in
guard let url = url, let data = try? Data(contentsOf: url) else { return }
let gif = UIImage(data: data)
}
2, 각 화상의 표시 시간을 개별적으로 설정하고 싶은 경우
let gifFrames: [GIFCreator.GIFFrame] = [
GIFCreator.GIFFrame(image: UIImage(), duration: 0.1),
GIFCreator.GIFFrame(image: UIImage(), duration: 0.2),
GIFCreator.GIFFrame(image: UIImage(), duration: 0.3)
]
GIFCreator.create(with: gifFrames) { url in
guard let url = url, let data = try? Data(contentsOf: url) else { return }
let gif = UIImage(data: data)
}
Reference
이 문제에 관하여(UIImage에서 GIF 생성), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/PowHu/items/c1a916d1514e6231f5ff텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)