3

I have the following ViewController class I am trying to get to display a live photo. I am not precisely sure how to actually get the image to show In dealing with a UIImageView I would write something like this:

let image = UIImage(imageNamed: "someStringPointingToImage") //then add this to the image view.

But does anyone know how it works with a live image that I have added in my assets?

The function

import UIKit
import Photos;
import PhotosUI
import MobileCoreServices

class ViewController: UIViewController, PHLivePhotoViewDelegate {

    var livePhotoIsAnimating = Bool()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.livePhotoView()
    }

    //MARK: Create the Live Photoview
    func livePhotoView() {

        let photoView: PHLivePhotoView = PHLivePhotoView.init(frame: self.view.bounds)
        //%%%%%%% Area to add the image %%%%%% 
        photoView.contentMode = .ScaleAspectFill

        self.view.addSubview(photoView)
        self.view.sendSubviewToBack(photoView)


    }


    func livePhotoView(livePhotoView: PHLivePhotoView, willBeginPlaybackWithStyle playbackStyle: PHLivePhotoViewPlaybackStyle) {


        self.livePhotoIsAnimating = true

    }

    func livePhotoView(livePhotoView: PHLivePhotoView, didEndPlaybackWithStyle playbackStyle: PHLivePhotoViewPlaybackStyle) {

        self.livePhotoIsAnimating = false

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Thanks.

Gugulethu
  • 1,426
  • 3
  • 18
  • 36

1 Answers1

4

Live Photos are a construct of the Photos app. The only place you can get a Live Photo is from the Photos framework (which, in turn, gets it from the user's Photos library). There's no affordance for storing Live Photos as bundle resources in your app.

If you want to present animated images, just use the UIImage constructors that create animated images from a set of frames, and display them in a UIImageView. Add your own gesture recognizers and you can easily create a UI that presents a static image by default and an animated one when tapped/pressed/whatever.

If you want to present video, look into AVKit.

rickster
  • 124,678
  • 26
  • 272
  • 326