Skip to content

Commit

Permalink
video file size check.
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Nov 15, 2017
1 parent b7e3298 commit 839f9ea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Example/TLPhotoPicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class ViewController: UIViewController,TLPhotosPickerViewControllerDelegate {

func getFirstSelectedImage() {
if let asset = self.selectedAssets.first {
if asset.type == .video {
asset.videoSize(completion: { [weak self] (size) in
self?.label.text = "video file size\(size)"
})
return
}
if let image = asset.fullResolutionImage {
print(image)
self.label.text = "local storage image"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public struct TLPHAsset {
public var type: AssetType
// get full resolution image
public var fullResolutionImage: UIImage?
// get video file size (async)
public func videoSize(options: PHVideoRequestOptions? = nil, completion: @escaping ((Int)->Void))
// get async icloud image (download)
@discardableResult
public func cloudImageDownload(progressBlock: @escaping (Double) -> Void, completionBlock:@escaping (UIImage?)-> Void ) -> PHImageRequestID?
Expand Down
2 changes: 1 addition & 1 deletion TLPhotoPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'TLPhotoPicker'
s.version = '1.3.5'
s.version = '1.3.6'
s.summary = 'multiple phassets picker for iOS lib. like facebook'

# This description is used to generate tags and improve search results.
Expand Down
22 changes: 22 additions & 0 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ public struct TLPHAsset {
return resource.originalFilename
}
}

public func videoSize(options: PHVideoRequestOptions? = nil, completion: @escaping ((Int)->Void)) {
guard let phAsset = self.phAsset, self.type == .video else { return }
PHImageManager.default().requestAVAsset(forVideo: phAsset, options: options) { (avasset, audioMix, info) in
func fileSize(_ url: URL?) -> Int? {
do {
guard let fileSize = try url?.resourceValues(forKeys: [.fileSizeKey]).fileSize else { return nil }
return fileSize
}catch { return nil }
}
var url: URL? = nil
if let urlAsset = avasset as? AVURLAsset {
url = urlAsset.url
}else if let sandboxKeys = info?["PHImageFileSandboxExtensionTokenKey"] as? String, let path = sandboxKeys.components(separatedBy: ";").last {
url = URL(fileURLWithPath: path)
}
let size = fileSize(url) ?? -1
DispatchQueue.main.async {
completion(size)
}
}
}

init(asset: PHAsset?) {
self.phAsset = asset
Expand Down

0 comments on commit 839f9ea

Please sign in to comment.