Skip to content

Commit

Permalink
fixed the save for .gif images
Browse files Browse the repository at this point in the history
  • Loading branch information
cranci1 committed Feb 17, 2024
1 parent bbed634 commit 43b7b1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Binary file not shown.
5 changes: 5 additions & 0 deletions AnimeGen/Extensions/ImageExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ extension UIImage {

return images
}

var imageData: Data? {
return self.sd_imageData()
}

}

19 changes: 11 additions & 8 deletions AnimeGen/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,29 +272,32 @@ class ViewController: UIViewController {
}

@objc func heartButtonTapped() {
// Ensure imageView.image is not nil
guard let image = imageView.image else {
return
}

// Check if the image is a GIF using CGImageSource
if let data = image.jpegData(compressionQuality: 1.0),
if let data = image.imageData,
let source = CGImageSourceCreateWithData(data as CFData, nil),
let utType = CGImageSourceGetType(source),
UTTypeConformsTo(utType, kUTTypeGIF) {

// Image is a GIF, save the original image
DispatchQueue.main.async {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
PHPhotoLibrary.shared().performChanges {
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: .photo, data: data, options: nil)
} completionHandler: { (success, error) in
if success {
print("GIF image saved to Photos library")
self.animateFeedback()
} else {
print("Error saving GIF image: \(error?.localizedDescription ?? "")")
}
}
return
}

// Image is not a GIF, convert to JPEG format
if let imageData = image.jpegData(compressionQuality: 1.0),
let uiImage = UIImage(data: imageData) {

// Save the converted image
DispatchQueue.main.async {
UIImageWriteToSavedPhotosAlbum(uiImage, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
Expand Down

0 comments on commit 43b7b1a

Please sign in to comment.