1

I'm creating a instagram like feature, where you can add Draggable text, stickers and gifs. I am able to add text and stickers and save it by taking a screenshot.

Is there a way to save an image with an animated gif added to it.

Can someone suggest a solution?

Thanks.

Len_X
  • 825
  • 11
  • 29
Caleb
  • 295
  • 3
  • 15
  • Please specify what exactly you trying to achieve and what you struggling with? Do you want to save gif on device? Or just static version of it? – ManWithBear Mar 20 '19 at 12:42
  • yes want to save gif on device.not static one. – Caleb Mar 20 '19 at 12:51
  • @Caleb hi is it still relevant? if so - could you please specify, do you want to use created animations only inside your app, or you want to save it to Photos gallery? – ATV Oct 15 '19 at 12:12

1 Answers1

0

Hi I achieved this using below method, 
I hope it helps to someone

1.After placing gif on Image, I am taking screenshot of holderView by this method, using timer.


@objc func gifScreenShot() {
      guard let imageView = self.screenView else {
         return
      }

      UIGraphicsBeginImageContextWithOptions(imageView.frame.size, true, 0.0)
      imageView.drawHierarchy(in: imageView.bounds, afterScreenUpdates: true)
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      self.gifImages.append(image!)

      if gifImages.count == 50 {
         self.stopTimer()
      }
   }

2.Once 50 screenshot taken, I stop the timer

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

3.And combine all screenshots to 0.05 secs video and save it photo gallery


   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.loadingViewStop()
               self.gifImages.removeAll()
               if saved {
                  self.gifImages.removeAll()
                  DispatchQueue.main.async {
                     self.view.toastMessage("Video saved successfully".localized)
                  }
               } else {
                  print(error)
               }
            }
         } else {
            self.gifImages.removeAll()
            self.videoFileURL = fileURL
            self.nextTapped(snap: nil)
         }
      }
   }

It worked like I expected

Caleb
  • 295
  • 3
  • 15