Ale*_*son 5 image-processing gif animated-gif ios swift3
我已经了解到,根据文档,自从iOS 9以来,Image IO Framework已经在语法上发生了变化,但是我已经完成了我的研究,以下代码似乎是正确的.我必须遵守下列程序; 一个过程拍摄图像并将这些图像作为gif写入应用程序的文档文件夹.我可以确认这是有效的,因为如果我使用iTunes转到应用程序的文档文件夹,我可以查看实际的gif文件.尽管如此,在我尝试从同一个文件中读取的第二个过程中,抛出一个错误,指出该路径上的文件不存在.我已经发布了以下代码.
class GifManager {
private func getDocumentsDirectory() -> URL? {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
}
public func generateGif(photos: [UIImage], filename: String) -> Bool {
if let docsDirectory = getDocumentsDirectory() {
let url = docsDirectory.appendingPathComponent(filename)
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.125]]
if let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, photos.count, nil) {
CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
for photo in photos {
CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
}
return CGImageDestinationFinalize(destination)
}
}
return false
}
public func saveGifToCameraRoll(filename: String) {
if let docsDirectory = getDocumentsDirectory() {
let fileUrl: URL = docsDirectory.appendingPathComponent(filename)
do {
let data = try Data(contentsOf: fileUrl)
if let _ = UIImage(data: data) {
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: fileUrl)
}, completionHandler: {completed, error in
if error != nil {
print("error")
} else if completed {
print("completed")
} else {
print("not completed")
}
})
}
} catch let error {
print(error)
}
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.1
对于那些想要更新版本的GIF生成功能的人,我已将其包含在此处.
此函数需要ImageIO和MobileCoreServicesimport语句.
import ImageIO
import MobileCoreServices
Run Code Online (Sandbox Code Playgroud)
这是功能.
func generateGif(photos: [UIImage], filename: String) -> Bool {
let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = documentsDirectoryPath.appending(filename)
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.125]]
let cfURL = URL(fileURLWithPath: path) as CFURL
if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, photos.count, nil) {
CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
for photo in photos {
CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
}
return CGImageDestinationFinalize(destination)
}
return false
}
Run Code Online (Sandbox Code Playgroud)
编辑:
它有一个Bool你知道你可以安全地使用它创建的文件.
if generateGif(arrayOfImages, "/myGIFfile.gif") {
// do something with gif
} else {
// failed to create and close the gif file
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3171 次 |
| 最近记录: |