In my animation app, I want to save the animation as a gif to the photos app. Right now, the code I have successfully saves it to photos the first time, but after that it generates an error (PHPhotosErrorDomain error -1.) The generateGifFromImages function seems to work because I can find all the gif files in the destination folder, but (besides the first time I do it) they just aren't saving to photos.
I am using this code to create a gif from an array of images:
import UIKit
import MobileCoreServices
public class GIFFromImages {
public enum colorSpace {
case rgb
case gray
}
public init () {}
}
extension GIFFromImages {
public func makeFileURL(filename: String) -> URL {
let gifDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let gifFileURL = gifDirectory.appendingPathComponent(filename)
return gifFileURL
}
public func generateGifFromImages(images: [UIImage], fileURL: URL, colorSpace: colorSpace, delayTime: Double, loopCount: Int) {
let gifGroup = DispatchGroup()
var tempImages: [UIImage] = []
for image in images {
gifGroup.enter()
let imageWidth = UIScreen.main.bounds.width
let imageHeight = UIScreen.main.bounds.height
let imageRect: CGRect = CGRect(x:0, y:0, width: imageWidth, height: imageHeight)
let imageBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context = CGContext(data: nil, width: Int(imageWidth), height: Int(imageHeight), bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceGray(), bitmapInfo: imageBitmapInfo.rawValue)
if let cgImg = image.cgImage {
//Set bg as white
context?.setFillColor(UIColor.white.cgColor)
context?.fill(imageRect)
context?.draw(cgImg, in: imageRect)
if let makeImg = context?.makeImage() {
let imageRef = makeImg
let newImage = UIImage(cgImage: imageRef)
tempImages.append(newImage)
gifGroup.leave()
}
}
}
gifGroup.notify(queue: .main) {
let gifFileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]] as CFDictionary
let gifFrameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: delayTime]] as CFDictionary
if let url = fileURL as CFURL? {
if let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, nil) {
CGImageDestinationSetProperties(destination, gifFileProperties)
for image in tempImages {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, gifFrameProperties)
}
}
if !CGImageDestinationFinalize(destination) {
print("Failed to finalize the image destination")
}
}
}
}
}
}
And I am calling it and saving to photos from this button:
Button {
print("Saving animation titled: \(animation.title)")
var images: [UIImage] = []
for frame in animation.frames {
let image = try! PKDrawing(data: frame.frameData).generateThumbnail(scale: 1)
images.append(image)
}
let timestamp = Date().timeIntervalSince1970
let timestampString = String(format: "%.0f", timestamp)
let gifURL = gifManager.makeFileURL(filename: "\(timestampString).gif")
gifManager.generateGifFromImages(images: images, fileURL: gifURL, colorSpace: .rgb, delayTime: (1.0/Double(animation.framesPerSecond)), loopCount: 0)
// Save the generated GIF to the Photos library
PHPhotoLibrary.shared().performChanges({
// Check if the GIF file exists
if FileManager.default.fileExists(atPath: gifURL.path) {
print("GIF file exists at:", gifURL.path)
} else {
print("Error: GIF file does not exist at:", gifURL.path)
}
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: gifURL)
}) { success, error in
if success {
print("GIF saved successfully.")
} else {
print("Error saving GIF:", error?.localizedDescription ?? "Unknown error")
}
}
close()
} label: {
ZStack {
Rectangle()
.frame(width: 400, height: 100)
.foregroundColor(.accentColor)
.cornerRadius(30)
Text("Save GIF to Photos")
.foregroundStyle(.white)
.font(.title)
.bold()
}
}
I figured out a solution -- I made the fileName the same for all the files that I save (so it overrides the last one instead of adding extra storage), then I added a completion handler to the generateGifFromImages function, and then followed this answer to replace my code for saving the gif to Photos: Save gif to iOS Photo Library in Swift