PHAsset에서 동영상 데이터를 검색하고 재생
소개
이 기사에서는 PhotoKit거친 절차는 다음과 같은 느낌입니다!
1. PHAsset를 사용하여 PhotoKit에서 자산 데이터 검색
2. Photo Library를 사용하여 동영상 데이터 검색
3. 검색된 동영상 데이터를 PHImageManager로 설정
완성 이미지는 다음과 같은 느낌입니다!

전체 소스 코드는 다음입니다!
htps : // 기주 b. 이 m/h-타로/P
환경
AVPlayerLayerXcode 12.5.1Swift 5.4.2
구체적인 구현 방법
이번 간이 되는 부분은 다음 코드입니다!
AssetViewController.swift@IBAction func playVideo(_ sender: Any) {
if playerLayer != nil {
playerLayer?.player?.play()
} else {
let options = PHVideoRequestOptions()
options.deliveryMode = .automatic
PHImageManager.default().requestPlayerItem(forVideo: asset, options: options) { playerItem, _ in
guard let playerItem = playerItem else {
fatalError("playerItem is nil")
}
// playerItemに動画データが含まれています
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = self.view.layer.bounds
self.view.layer.addSublayer(playerLayer)
playerLayer.player?.play()
self.playerLayer = playerLayer
}
}
}
참고 링크
Reference
이 문제에 관하여(PHAsset에서 동영상 데이터를 검색하고 재생), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/h-taro/items/edff550f1731476b154f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
AVPlayerLayerXcode 12.5.1Swift 5.4.2구체적인 구현 방법
이번 간이 되는 부분은 다음 코드입니다!
AssetViewController.swift@IBAction func playVideo(_ sender: Any) {
if playerLayer != nil {
playerLayer?.player?.play()
} else {
let options = PHVideoRequestOptions()
options.deliveryMode = .automatic
PHImageManager.default().requestPlayerItem(forVideo: asset, options: options) { playerItem, _ in
guard let playerItem = playerItem else {
fatalError("playerItem is nil")
}
// playerItemに動画データが含まれています
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = self.view.layer.bounds
self.view.layer.addSublayer(playerLayer)
playerLayer.player?.play()
self.playerLayer = playerLayer
}
}
}
참고 링크
Reference
이 문제에 관하여(PHAsset에서 동영상 데이터를 검색하고 재생), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/h-taro/items/edff550f1731476b154f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
@IBAction func playVideo(_ sender: Any) {
if playerLayer != nil {
playerLayer?.player?.play()
} else {
let options = PHVideoRequestOptions()
options.deliveryMode = .automatic
PHImageManager.default().requestPlayerItem(forVideo: asset, options: options) { playerItem, _ in
guard let playerItem = playerItem else {
fatalError("playerItem is nil")
}
// playerItemに動画データが含まれています
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = self.view.layer.bounds
self.view.layer.addSublayer(playerLayer)
playerLayer.player?.play()
self.playerLayer = playerLayer
}
}
}
Reference
이 문제에 관하여(PHAsset에서 동영상 데이터를 검색하고 재생), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/h-taro/items/edff550f1731476b154f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)