PHAssetResourceInputStream is a library that adds input stream support for assets from Photos Framework. It is build on the top of POSInputStreamLibrary and it uses bounded blocking queue under the hood to inverse the producer-like API of PHAssetResourceManager and makes it consumer-friendly.
Creating a stream:
let inputStream = InputStream.inputStream(withAssetResource: assetResource)Setting an offset:
inputStream.setProperty(100, forKey: .fileCurrentOffsetKey)Furthemore, the library provides a set of data structures that allows you to work with data of PHAssetResource in a synchronous manner.
Bellow is the example of getting the size from a PHAssetResource:
let dataProducer = PHAssetResourceDataProducer(assetResource: assetResource)
let dataGenerator = DataGeneratorFromDataProducer(dataProducer: dataProducer)
do {
var size = 0
while let data = try dataGenerator.nextChunk() {
size += data.length
}
print(size)
} catch let error {
print("Error occured: \(error)")
}- [-] Add an example project
PHAssetResourceInputStream is available through CocoaPods. To install, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'PHAssetResourceInputStream'
endPHAssetResourceInputStream is available under the MIT license. See the LICENSE file for more info.