⬆️ ⬇️

iOS: Work with the gallery (Photos framework). Part 2

Hi, Habr! In this article I will tell you about working with video, live photos and tracking changes in the gallery using the Photos framework. For a better understanding of the article I recommend to read the previous article .





Work with video



In order to upload a video from the gallery, you must set the request parameters (options) - PHVideoRequestOptions.



let options = PHVideoRequestOptions() // ,  PHImageManager     iCloud options.isNetworkAccessAllowed = true //     options.deliveryMode = .automatic options.progressHandler = { progress, _, _, _ in //       } 


Further, these parameters together with the PHAsset object (see previous article ) are passed to the following function:

')

 PHImageManager.default().requestPlayerItem(forVideo: asset, options: options, resultHandler: { playerItem, info in //    playerItem } 


Once received playerItem, you can play it. To play the video, use the AVPlayerLayer and AVPlayer classes:



 let player = AVPlayer(playerItem: playerItem) let playerLayer = AVPlayerLayer(player: player) //  playerLayer     view player.play() 


Work with live photo



Working with live photos and videos are similar. To begin with, we also set the request parameters, only now using the PHLivePhotoRequestOptions class:



 let options = PHLivePhotoRequestOptions() options.isNetworkAccessAllowed = true options.deliveryMode = .highQualityFormat options.progressHandler = { progress, _, _, _ in //       } 


Then load the live photo:



 PHImageManager.default().requestLivePhoto(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { livePhoto, info in //    livePhoto }) 


After receiving the livePhoto object, we can display it in a special component PHLivePhotoView. This component is very similar to UIImageView. It is designed to display live photos.



 livePhotoView.livePhoto = livePhoto //  live- livePhotoView.startPlayback(with: .full) 


Tracking gallery changes



Photos framework allows you to follow the changes that have occurred both with albums and with individual photos or videos. To start tracking changes you need to add an observer to the PHPhotoLibrary:



 PHPhotoLibrary.shared().register(observer) 


The observer must implement the PHPhotoLibraryChangeObserver protocol, which has only one method - photoLibraryDidChange (_ changeInstance: PHChange). The changeInstance parameter is an object that contains a list of changes that occurred in the gallery.



As an example, I will show how to update the collectionView in accordance with the changes.



 func photoLibraryDidChange(_ changeInstance: PHChange) { //    PHAsset ( fetchResult) //    ,     guard let changes = changeInstance.changeDetails(for: fetchResult) else { return } //      main queue,   DispatchQueue.main.sync { //    PHAsset fetchResult = changes.fetchResultAfterChanges //  -        // (, , , ) if changes.hasIncrementalChanges { //    collectionView collectionView.performBatchUpdates({ //     : // delete, insert, reload, move if let removed = changes.removedIndexes where removed.count > 0 { collectionView.deleteItems(at: removed.map { IndexPath(item: $0, section:0) }) } if let inserted = changes.insertedIndexes where inserted.count > 0 { collectionView.insertItems(at: inserted.map { IndexPath(item: $0, section:0) }) } if let changed = changes.changedIndexes where changed.count > 0 { collectionView.reloadItems(at: changed.map { IndexPath(item: $0, section:0) }) } changes.enumerateMoves { fromIndex, toIndex in collectionView.moveItem(at: IndexPath(item: fromIndex, section: 0), to: IndexPath(item: toIndex, section: 0)) } }) } else { //   collectionView collectionView.reloadData() } } } 


Conclusion



The code given in the articles is not ideal. Its main task is to show the mechanism of the Photos framework. If something was unclear - ask in the comments. Perhaps, I will do a little later on the github repository with the project, where I will put all the code together.

Thanks for reading.

Source: https://habr.com/ru/post/319240/



All Articles