CGImageDestinationCreateWithURL 的问题
Problems with CGImageDestinationCreateWithURL
我正在使用 CGImageDestinationCreateWithURL 来保存 gif 图像,这是我的代码:
func gifFromImages(images: Array<UIImage>, animationTime: Double) -> String? {
let date = Int(NSDate().timeIntervalSince1970)
let path = Bundle.main.bundlePath + "/gifs/" + String(date) + ".gif"
let delay = animationTime / Double(totalFrames)
let prep = [kCGImagePropertyGIFDictionary as String :
[kCGImagePropertyGIFDelayTime as String : delay]] as CFDictionary
let fileProperties = [ kCGImagePropertyGIFDictionary as String :
[kCGImagePropertyGIFLoopCount as String : 0] ,
kCGImageMetadataShouldExcludeGPS as String: true]
as CFDictionary
let url = URL(fileURLWithPath: path) as CFURL
guard let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF,
images.count, nil) else {
return nil
}
CGImageDestinationSetProperties(destination, fileProperties)
for image in images {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, prep)
}
}
if CGImageDestinationFinalize(destination) {
return path
}
return nil
}
我面临的问题是,当我在模拟器上尝试此代码时,它工作正常并且创建了我需要的文件,但是当我 运行 在设备上使用此代码时,CGImageDestinationCreateWith URL return 无。
我的第一个想法是文件的路径不正确,我在网上查看并尝试使用以下 paths/urls:
let path = Bundle.main.bundlePath + "/gifs/" + String(date) + ".gif"
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!.appending("/gifs/\(String(date)).gif")
let path = NSString(string: "~/gifs/\(String(date)).gif").expandingTildeInPath
let url = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) as CFURL
当我尝试使用 URL(string: path) 而不是 URL(fileURLWithPath:) 时,出现以下错误:
: CGDataConsumer(url_close): write failed.
知道会发生什么吗?我应该使用哪条路径?
我会回答这个问题,以防其他人遇到这个问题。经过一些研究并使用评论中的信息,我找到了解决方案。我最终使用了:
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true).last?.appending("/\(String(date)).gif")
我的第一个解决方案的问题是它在文档目录和文件名之后添加了一个文件夹。
我正在使用 CGImageDestinationCreateWithURL 来保存 gif 图像,这是我的代码:
func gifFromImages(images: Array<UIImage>, animationTime: Double) -> String? {
let date = Int(NSDate().timeIntervalSince1970)
let path = Bundle.main.bundlePath + "/gifs/" + String(date) + ".gif"
let delay = animationTime / Double(totalFrames)
let prep = [kCGImagePropertyGIFDictionary as String :
[kCGImagePropertyGIFDelayTime as String : delay]] as CFDictionary
let fileProperties = [ kCGImagePropertyGIFDictionary as String :
[kCGImagePropertyGIFLoopCount as String : 0] ,
kCGImageMetadataShouldExcludeGPS as String: true]
as CFDictionary
let url = URL(fileURLWithPath: path) as CFURL
guard let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF,
images.count, nil) else {
return nil
}
CGImageDestinationSetProperties(destination, fileProperties)
for image in images {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, prep)
}
}
if CGImageDestinationFinalize(destination) {
return path
}
return nil
}
我面临的问题是,当我在模拟器上尝试此代码时,它工作正常并且创建了我需要的文件,但是当我 运行 在设备上使用此代码时,CGImageDestinationCreateWith URL return 无。 我的第一个想法是文件的路径不正确,我在网上查看并尝试使用以下 paths/urls:
let path = Bundle.main.bundlePath + "/gifs/" + String(date) + ".gif"
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!.appending("/gifs/\(String(date)).gif")
let path = NSString(string: "~/gifs/\(String(date)).gif").expandingTildeInPath
let url = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) as CFURL
当我尝试使用 URL(string: path) 而不是 URL(fileURLWithPath:) 时,出现以下错误:
: CGDataConsumer(url_close): write failed.
知道会发生什么吗?我应该使用哪条路径?
我会回答这个问题,以防其他人遇到这个问题。经过一些研究并使用评论中的信息,我找到了解决方案。我最终使用了:
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true).last?.appending("/\(String(date)).gif")
我的第一个解决方案的问题是它在文档目录和文件名之后添加了一个文件夹。