在 Playground 中使用实况照片
Working with Live Photos in Playground
我已经在网上进行了大量搜索,但我目前正尝试在 Playground 中使用 "Live Photos"。我知道框架 (PHLivePhoto),我只是不知道是否可以在 Playground 中使用它们,因为 "import" 似乎没有什么 [=12] =] 可在线下载。有什么想法吗?
可以在 Playground 根据实际 Live Photo 的元素制作和查看 PHLivePhoto。
在 OS X 的 Photos
应用中,select 一张实况照片并转到菜单
File > Export > Export original...
它将创建一个 .JPG
和一个 .mov
。
将这两个文件拖放到 Playground 的 Resources
文件夹中(菜单视图 > 导航器 > 显示项目导航器)。
使用 NSBundle
获取这两个文件的 URL(在我的示例中,文件是 "IMG_0001.JPG" 和 "IMG_0001.mov"):
let imgURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!
let movURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!
并创建一个实际图像,我们将需要它作为 Live Photo 预览图像:
let prevImg = UIImage(named: "IMG_0001.JPG")!
导入必要的框架:
import Photos
import PhotosUI
import XCPlayground
并将 Playground 设置为异步模式:
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
现在我们将使用 PHLivePhoto
的 requestLivePhotoWithResourceFileURLs
方法从我们的元素创建 PHLivePhoto:
func makeLivePhotoFromItems(imageURL: NSURL, videoURL: NSURL, previewImage: UIImage, completion: (livePhoto: PHLivePhoto) -> Void) {
PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: previewImage, targetSize: CGSizeZero, contentMode: PHImageContentMode.AspectFit) {
(livePhoto, infoDict) -> Void in
// for debugging: print(infoDict)
if let lp = livePhoto {
completion(livePhoto: lp)
}
}
}
然后我们这样调用:
makeLivePhotoFromItems(imgURL, videoURL: movURL, previewImage: prevImg) { (livePhoto) -> Void in
// "livePhoto" is your PHLivePhoto object
}
例如,假设您希望 Playground 进行实时取景:
makeLivePhotoFromItems(imgURL, videoURL: movURL, previewImage: prevImg) { (livePhoto) -> Void in
let rect = CGRect(x: 0, y: 0, width: 2048, height: 1536)
let livePhotoView = PHLivePhotoView(frame: rect)
livePhotoView.livePhoto = livePhoto
XCPlaygroundPage.currentPage.liveView = livePhotoView
livePhotoView.startPlaybackWithStyle(PHLivePhotoViewPlaybackStyle.Full)
}
请注意,由于无法与实时视图交互以开始播放实时照片,我们必须使用 PHLivePhotoView
的 startPlaybackWithStyle
方法自行完成。
您可以通过在菜单中显示助理编辑器来强制在 Playground 中显示实时视图
View > Assistant Editor > Show Assistant Editor
注意:Playground 创建 PHLivePhoto 并启动实时取景可能需要一些时间。
有了 Xcode 7.3b+ 我们终于可以在 Playgrounds 中进行一些 UI 互动了。
我已经通过简单的视图和 touchesBegan 对这个答案进行了改编,只需在控制台显示时单击 LivePhoto:
import UIKit
import XCPlayground
import Photos
import PhotosUI
class PLView: UIView {
let image: UIImage
let imageURL: NSURL
let videoURL: NSURL
let liveView: PHLivePhotoView
init(image: UIImage, imageURL: NSURL, videoURL: NSURL) {
self.image = image
self.imageURL = imageURL
self.videoURL = videoURL
let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
self.liveView = PHLivePhotoView(frame: rect)
super.init(frame: rect)
self.addSubview(self.liveView)
}
func prepareLivePhoto() {
makeLivePhotoFromItems { (livePhoto) in
self.liveView.livePhoto = livePhoto
print("\nReady! Click on the LivePhoto in the Assistant Editor panel!\n")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("\nClicked! Wait for it...\n")
self.liveView.startPlaybackWithStyle(.Full)
}
private func makeLivePhotoFromItems(completion: (PHLivePhoto) -> Void) {
PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: image, targetSize: CGSizeZero, contentMode: .AspectFit) {
(livePhoto, infoDict) -> Void in
// This "canceled" condition is just to avoid redundant passes in the Playground preview panel.
if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? Int where canceled == 0 {
if let livePhoto = livePhoto {
completion(livePhoto)
}
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let plview = PLView(image: UIImage(named: "IMG_0001.JPG")!,
imageURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!,
videoURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!)
XCPlaygroundPage.currentPage.liveView = plview
plview.prepareLivePhoto()
Swift 3.0.2 的相同示例(Xcode 8.2.1):
import UIKit
import PlaygroundSupport
import Photos
import PhotosUI
class PLView: UIView {
let image: UIImage
let imageURL: URL
let videoURL: URL
let liveView: PHLivePhotoView
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(image: UIImage, imageURL: URL, videoURL: URL) {
self.image = image
self.imageURL = imageURL
self.videoURL = videoURL
let rect = CGRect(x: 0, y: 0, width: 300, height: 400)
self.liveView = PHLivePhotoView(frame: rect)
super.init(frame: rect)
self.addSubview(self.liveView)
}
func prepareLivePhoto() {
makeLivePhotoFromItems { (livePhoto) in
self.liveView.livePhoto = livePhoto
print("\nReady! Click on the LivePhoto in the Assistant Editor panel!\n")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\nClicked! Wait for it...\n")
self.liveView.startPlayback(with: .full)
}
private func makeLivePhotoFromItems(completion: @escaping (PHLivePhoto) -> Void) {
PHLivePhoto.request(withResourceFileURLs: [imageURL, videoURL], placeholderImage: image, targetSize: CGSize.zero, contentMode: .aspectFit) {
(livePhoto, infoDict) -> Void in
if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? NSNumber,
canceled == 0,
let livePhoto = livePhoto
{
completion(livePhoto)
}
}
}
}
let plview = PLView(image: UIImage(named: "IMG_0001.JPG")!,
imageURL: Bundle.main.url(forResource: "IMG_0001", withExtension: "JPG")!,
videoURL: Bundle.main.url(forResource: "IMG_0001", withExtension: "mov")!)
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = plview
plview.prepareLivePhoto()
我已经在网上进行了大量搜索,但我目前正尝试在 Playground 中使用 "Live Photos"。我知道框架 (PHLivePhoto),我只是不知道是否可以在 Playground 中使用它们,因为 "import" 似乎没有什么 [=12] =] 可在线下载。有什么想法吗?
可以在 Playground 根据实际 Live Photo 的元素制作和查看 PHLivePhoto。
在 OS X 的 Photos
应用中,select 一张实况照片并转到菜单
File > Export > Export original...
它将创建一个 .JPG
和一个 .mov
。
将这两个文件拖放到 Playground 的 Resources
文件夹中(菜单视图 > 导航器 > 显示项目导航器)。
使用 NSBundle
获取这两个文件的 URL(在我的示例中,文件是 "IMG_0001.JPG" 和 "IMG_0001.mov"):
let imgURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!
let movURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!
并创建一个实际图像,我们将需要它作为 Live Photo 预览图像:
let prevImg = UIImage(named: "IMG_0001.JPG")!
导入必要的框架:
import Photos
import PhotosUI
import XCPlayground
并将 Playground 设置为异步模式:
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
现在我们将使用 PHLivePhoto
的 requestLivePhotoWithResourceFileURLs
方法从我们的元素创建 PHLivePhoto:
func makeLivePhotoFromItems(imageURL: NSURL, videoURL: NSURL, previewImage: UIImage, completion: (livePhoto: PHLivePhoto) -> Void) {
PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: previewImage, targetSize: CGSizeZero, contentMode: PHImageContentMode.AspectFit) {
(livePhoto, infoDict) -> Void in
// for debugging: print(infoDict)
if let lp = livePhoto {
completion(livePhoto: lp)
}
}
}
然后我们这样调用:
makeLivePhotoFromItems(imgURL, videoURL: movURL, previewImage: prevImg) { (livePhoto) -> Void in
// "livePhoto" is your PHLivePhoto object
}
例如,假设您希望 Playground 进行实时取景:
makeLivePhotoFromItems(imgURL, videoURL: movURL, previewImage: prevImg) { (livePhoto) -> Void in
let rect = CGRect(x: 0, y: 0, width: 2048, height: 1536)
let livePhotoView = PHLivePhotoView(frame: rect)
livePhotoView.livePhoto = livePhoto
XCPlaygroundPage.currentPage.liveView = livePhotoView
livePhotoView.startPlaybackWithStyle(PHLivePhotoViewPlaybackStyle.Full)
}
请注意,由于无法与实时视图交互以开始播放实时照片,我们必须使用 PHLivePhotoView
的 startPlaybackWithStyle
方法自行完成。
您可以通过在菜单中显示助理编辑器来强制在 Playground 中显示实时视图
View > Assistant Editor > Show Assistant Editor
注意:Playground 创建 PHLivePhoto 并启动实时取景可能需要一些时间。
有了 Xcode 7.3b+ 我们终于可以在 Playgrounds 中进行一些 UI 互动了。
我已经通过简单的视图和 touchesBegan 对这个答案进行了改编,只需在控制台显示时单击 LivePhoto:
import UIKit
import XCPlayground
import Photos
import PhotosUI
class PLView: UIView {
let image: UIImage
let imageURL: NSURL
let videoURL: NSURL
let liveView: PHLivePhotoView
init(image: UIImage, imageURL: NSURL, videoURL: NSURL) {
self.image = image
self.imageURL = imageURL
self.videoURL = videoURL
let rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
self.liveView = PHLivePhotoView(frame: rect)
super.init(frame: rect)
self.addSubview(self.liveView)
}
func prepareLivePhoto() {
makeLivePhotoFromItems { (livePhoto) in
self.liveView.livePhoto = livePhoto
print("\nReady! Click on the LivePhoto in the Assistant Editor panel!\n")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("\nClicked! Wait for it...\n")
self.liveView.startPlaybackWithStyle(.Full)
}
private func makeLivePhotoFromItems(completion: (PHLivePhoto) -> Void) {
PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: image, targetSize: CGSizeZero, contentMode: .AspectFit) {
(livePhoto, infoDict) -> Void in
// This "canceled" condition is just to avoid redundant passes in the Playground preview panel.
if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? Int where canceled == 0 {
if let livePhoto = livePhoto {
completion(livePhoto)
}
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let plview = PLView(image: UIImage(named: "IMG_0001.JPG")!,
imageURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!,
videoURL: NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!)
XCPlaygroundPage.currentPage.liveView = plview
plview.prepareLivePhoto()
Swift 3.0.2 的相同示例(Xcode 8.2.1):
import UIKit
import PlaygroundSupport
import Photos
import PhotosUI
class PLView: UIView {
let image: UIImage
let imageURL: URL
let videoURL: URL
let liveView: PHLivePhotoView
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(image: UIImage, imageURL: URL, videoURL: URL) {
self.image = image
self.imageURL = imageURL
self.videoURL = videoURL
let rect = CGRect(x: 0, y: 0, width: 300, height: 400)
self.liveView = PHLivePhotoView(frame: rect)
super.init(frame: rect)
self.addSubview(self.liveView)
}
func prepareLivePhoto() {
makeLivePhotoFromItems { (livePhoto) in
self.liveView.livePhoto = livePhoto
print("\nReady! Click on the LivePhoto in the Assistant Editor panel!\n")
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("\nClicked! Wait for it...\n")
self.liveView.startPlayback(with: .full)
}
private func makeLivePhotoFromItems(completion: @escaping (PHLivePhoto) -> Void) {
PHLivePhoto.request(withResourceFileURLs: [imageURL, videoURL], placeholderImage: image, targetSize: CGSize.zero, contentMode: .aspectFit) {
(livePhoto, infoDict) -> Void in
if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? NSNumber,
canceled == 0,
let livePhoto = livePhoto
{
completion(livePhoto)
}
}
}
}
let plview = PLView(image: UIImage(named: "IMG_0001.JPG")!,
imageURL: Bundle.main.url(forResource: "IMG_0001", withExtension: "JPG")!,
videoURL: Bundle.main.url(forResource: "IMG_0001", withExtension: "mov")!)
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = plview
plview.prepareLivePhoto()