我已经做了相当多的网络搜索,但我目前正在尝试与“现场照片”在游乐场。我知道这个框架(PHLivePhoto),我只是不知道在游乐场上和他们合作是否可能,因为没有什么可以“导入”,因为似乎没有任何“实时照片”可以在线下载。有什么想法吗?
发布于 2015-12-05 17:43:12
在游乐场上,可以从实际的PHLivePhoto元素中制作和查看PHLivePhoto。
在OS的Photos应用程序中,选择一个Live并转到菜单
文件>出口>出口原件.

它将创建一个.JPG和一个.mov。
将这两个文件放在游乐场的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预览图像:
let prevImg = UIImage(named: "IMG_0001.JPG")!导入必要的框架:
import Photos
import PhotosUI
import XCPlayground并将游乐场设置为异步模式:
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
}例如,假设您希望游乐场提供一个实时视图:
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)
}请注意,由于无法与实时视图交互来启动live的播放,所以我们必须使用PHLivePhotoView的startPlaybackWithStyle方法自己进行交互。
通过在菜单中显示“助理编辑器”,可以强制在游乐场中显示实时视图
视图>助理编辑器>显示助理编辑器

注意:游乐场创建PHLivePhoto和启动实时视图可能需要一些时间。
使用Xcode 7.3b+,我们最终可以在操场上进行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()https://stackoverflow.com/questions/33990830
复制相似问题