PreviewController
@objcMembers
public class PreviewController : UIViewController
Preview controllers let you draw any image or video based Session
object.
For example, you can use the PreviewController
class to display the contents of a session found in the savedSessions
array (user Drafts) or even a Session
you have just initialized.
You configure a preview controller programmatically by adding its view as a subview to the view of your choosing. For video, you can also use methods of this class to start or stop the video and specify other playback parameters.
The preview controller will also display programmatic edits that are made on a session in real-time.
Figure 1 Preview controller
A preview controller uses the contentMode
variable and the size of the media itself to determine how to display the media. The preview controller can scale your media to fit all or some of the available space. If the size of
the preview controller changes, it automatically scales the media as needed.
The presentation of the media is determined by the preview controllers contentMode
property. The contentAspectFit
and contentAspectFill
values scale the media to fit or fill the space while maintaining the media’s original aspect
ratio. The contentFill
value scales the media without regard to the original aspect ratio, which can cause the media to appear
distorted.
The below example creates a preview controller, adds it as a child of your controller and as a subview of your view. It then anchors the preview controller 30 points from the left of your view and 100 points from the top of your view. The preview controller is also given a height of 200 points and a width of 200 points.
override func viewDidLoad() {
super.viewDidLoad()
let controller = PreviewController()
self.addChild(controller)
self.view.addSubview(controller.view)
controller.view.translatesAutoresizingMaskIntoConstraints = false
controller.view.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 30).isActive = true
controller.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
controller.view.heightAnchor.constraint(equalToConstant: 200).isActive = true
controller.view.widthAnchor.constraint(equalToConstant: 200).isActive = true
controller.session = <#Your Session#>
}
-
Creates a
PreviewController
.Declaration
Swift
public init()
-
Set this delegate if you want to make changes to the preview controller view layout using the size of the media before or after it has appeared.
Default value:
nil
Declaration
Swift
public weak var delegate: PreviewControllerDelegate?
-
The session to be displayed by the preview controller.
All changes made to the session (changing filters, trim times, preferredTransform, etc.) will be automatically displayed by the preview controller in real-time.
Default value:
nil
Declaration
Swift
public var session: Session? { get set }
-
The preview controller uses the
contentMode
variable and the size of the media itself to determine how to display the media. The preview controller can scale your media to fit all or some of the available space.The
contentAspectFit
andcontentAspectFill
values scale the media to fit or fill the space while maintaining the media’s original aspect ratio.The
contentFill
value scales the media without regard to the original aspect ratio, which can cause the media to appear distorted.Default value:
.contentAspectFit
Declaration
Swift
public var contentMode: ContentMode { get set }
-
Controls whether or not the video should loop.
Default value:
true
Declaration
Swift
public var loops: Bool { get set }
-
Controls whether or not the video is muted.
Key value observable.
Default value:
false
Declaration
Swift
@objc dynamic public var isMuted: Bool { get set }
-
The current time of the video playback.
Default value:
.zero
Declaration
Swift
public var currentTime: CMTime { get }
-
The initial start time of video playback. When set, the video will start here once and on successive loops will start from the beginning of the video. If you need it to start here again, set this again.
Default value:
nil
Declaration
Swift
public var initialStartTime: CMTime? { get set }
-
When true, the video will automatically start playing after the
session
variable is set.Default value:
true
Declaration
Swift
public var autoplayEnabled: Bool
-
Indicates if the video is currently playing. The video may still be loading when this is true.
Default value:
false
Declaration
Swift
public private(set) var isPlaying: Bool { get }
-
Begins playback of the video.
Immediately after this is called,
isPlaying
will be true.Declaration
Swift
public func play()
-
Pauses playback of the video.
Immediately after this is called,
isPlaying
will be false.Declaration
Swift
public func pause()