如何将 gif 添加图像保存到相机胶卷?

How to save gif added image to camera roll?

我有一张图片查看图片。我正在打开 Giphy SDK 底部 sheet 以在我已添加的图像上添加 gif。

我使用以下代码创建 gif 视图以添加已添加的图像:

private func createGifView(url: String?) {
        let x = CGFloat.random(in: 0...(parentView.frame.width - 80))
        let y = CGFloat.random(in: 0...(parentView.frame.height - 80))
        let imageView = SDAnimatedImageView(frame: CGRect(x: x, y: y, width: 120, height: 120))
        imageView.isUserInteractionEnabled = true
        if let url = url {
            imageView.sd_setImage(with: URL(string: url), completed: nil)
        }
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(emojiDidMove))
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(emojiDidPinch))
        let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(emojiDidRotate))
        panGesture.delegate = self
        pinchGesture.delegate = self
        rotationGesture.delegate = self
        imageView.addGestureRecognizer(panGesture)
        imageView.addGestureRecognizer(pinchGesture)
        imageView.addGestureRecognizer(rotationGesture)
        parentView.addSubview(imageView)
        guard let rightBarButtonItems = navigationItem.rightBarButtonItems else { return }
        rightBarButtonItems[1].isEnabled = true
    }

我尝试使用此代码将添加的 gif 图片保存到相机胶卷:

private func saveEmojiAddedImage() -> UIImage {
        UIGraphicsBeginImageContext(parentView.frame.size)
        parentView.layer.render(in: UIGraphicsGetCurrentContext()!)
        guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage(named: "smile")! }

        let imageView = SDAnimatedImageView()
        let animatedImage = SDAnimatedImage(named: "image.gif")
        imageView.image = animatedImage

        imageView.image = image

        let imageData = imageView.image?.sd_imageData(as: .GIF)
        if let data = imageData {
        PHPhotoLibrary.shared().performChanges({
            PHAssetCreationRequest.forAsset().addResource(with: .photo, data: data, options: nil)
        }) { success, error in
            guard success else {
                print("failed to save gif \(error?.localizedDescription)")
                return
            }
            print("successfully saved gif")
        }
        }

        UIGraphicsEndImageContext()
        
        let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view
        self.present(activityViewController, animated: true, completion: nil)
        return image

    }

我正在使用此功能获取 giphy gif:

extension MainViewController: GiphyDelegate {
    func didSelectMedia(giphyViewController: GiphyViewController, media: GPHMedia) {
        let url = media.url(rendition: .fixedWidth, fileType: .gif)
        createGifView(url: url)
    }
}

我使用 Timer 截取了 25 张 gif 添加图像视图的屏幕截图,解决了我的问题。我将这25张截图添加到UIImage数组中,并将其转换为视频,最后以视频的形式保存到照片库中。

class MainViewController: UIViewController {
  private var gifImages: [UIImage] = []
  private var gifTimer: Timer?

  UIBarButtonItem(barButtonSystemItem: .save,
                            target: self,
                            action: #selector(fireTimer))

 @objc func fireTimer() {
        scheduledTimerWithTimeInterval()
    }

 private func scheduledTimerWithTimeInterval(){
        gifTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(saveEmojiAddedImage), userInfo: nil, repeats: true)
   }

  //MARK: - SaveEmojiAddedImage
  //Final Image
  @objc private func saveEmojiAddedImage() {
       UIGraphicsBeginImageContextWithOptions(parentView.frame.size, true, 0.0)
       parentView.drawHierarchy(in: parentView.bounds, afterScreenUpdates: true)            guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return }
        UIGraphicsEndImageContext()
        if isGIFAdded {
            self.gifImages.append(image)
            indicator.startAnimating()
            view.isUserInteractionEnabled = false
            print("Gif Images Count: \(gifImages.count)")
            if gifImages.count == 25 {
                self.stopTimer()
                indicator.stopAnimating()
                view.isUserInteractionEnabled = true
            }
        }
    }


    func stopTimer() {
        if gifTimer != nil {
            gifTimer!.invalidate()
            gifTimer = nil
            DispatchQueue.global(qos: .background).async {
                DispatchQueue.main.async {
                    self.createVideo()
                }
            }
        }
    }

使用 imageview 的 25 个屏幕截图创建视频

 func createVideo()  {
  let settings = CXEImagesToVideo.videoSettings(codec: AVVideoCodecType.h264.rawValue, width: (gifImages[0].cgImage?.width)!, height: (gifImages[0].cgImage?.height)!)
  let movieMaker = CXEImagesToVideo(videoSettings: settings)
  movieMaker.createMovieFrom(images: gifImages){ (fileURL:URL) in
      if self.isSaveVideo {
         PHPhotoLibrary.shared().performChanges({
         PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: fileURL)
           }) { saved, error in
                self.gifImages.removeAll()
                if saved {
                     self.gifImages.removeAll()
                     DispatchQueue.main.async {
                        self.presentAlert(withTitle: "Successful", message: "GIF saved successfully")
                     }
                  } else {
                     DispatchQueue.main.async {
                         self.presentAlert(withTitle: "Error", message: "")
                      }
                    }
                }
            } else {
                self.gifImages.removeAll()
                self.videoFileURL = fileURL
            }
        }
    }
}