Skip to content

Commit

Permalink
#Fixed 230
Browse files Browse the repository at this point in the history
  • Loading branch information
wade-hawk committed Oct 26, 2019
1 parent 367a871 commit e1ea38e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ public struct TLPHAsset {
// parmeter : convertLivePhotosToJPG
// false : If you want mov file at live photos
// true : If you want png file at live photos ( HEIC )
public func tempCopyMediaFile(videoRequestOptions: PHVideoRequestOptions? = nil, imageRequestOptions: PHImageRequestOptions? = nil, exportPreset: String = AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: Bool = false, progressBlock:((Double) -> Void)? = nil, completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID?
public func tempCopyMediaFile(videoRequestOptions: PHVideoRequestOptions? = nil,
imageRequestOptions: PHImageRequestOptions? = nil,
livePhotoRequestOptions: PHLivePhotoRequestOptions? = nil,
exportPreset: String = AVAssetExportPresetHighestQuality,
convertLivePhotosToJPG: Bool = false,
progressBlock:((Double) -> Void)? = nil,
completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID?
//Apparently, This is not the only way to export video.
//There is many way that export a video.
//This method was one of them.
Expand Down
56 changes: 52 additions & 4 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,48 @@ public struct TLPHAsset {
return nil
}

private func tempCopyLivePhotos(phAsset: PHAsset,
livePhotoRequestOptions: PHLivePhotoRequestOptions? = nil,
localURL: URL,
completionBlock:@escaping (() -> Void)) -> PHImageRequestID? {
var requestOptions = PHLivePhotoRequestOptions()
if let options = livePhotoRequestOptions {
requestOptions = options
}else {
requestOptions.isNetworkAccessAllowed = true
}
return PHImageManager.default().requestLivePhoto(for: phAsset,
targetSize: UIScreen.main.bounds.size,
contentMode: .default,
options: requestOptions)
{ (livePhotos, infoDict) in
if let livePhotos = livePhotos {
let assetResources = PHAssetResource.assetResources(for: livePhotos)
var videoResources: PHAssetResource?
assetResources.forEach { (resource) in
if resource.type == .pairedVideo {
PHAssetResourceManager.default().writeData(for: resource, toFile: localURL, options: nil) { (error) in
DispatchQueue.main.async {
completionBlock()
}
}
}
}
}
}
}

@discardableResult
//convertLivePhotosToJPG
// false : If you want mov file at live photos
// true : If you want png file at live photos ( HEIC )
public func tempCopyMediaFile(videoRequestOptions: PHVideoRequestOptions? = nil, imageRequestOptions: PHImageRequestOptions? = nil, exportPreset: String = AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: Bool = false, progressBlock:((Double) -> Void)? = nil, completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID? {
public func tempCopyMediaFile(videoRequestOptions: PHVideoRequestOptions? = nil,
imageRequestOptions: PHImageRequestOptions? = nil,
livePhotoRequestOptions: PHLivePhotoRequestOptions? = nil,
exportPreset: String = AVAssetExportPresetHighestQuality,
convertLivePhotosToJPG: Bool = false,
progressBlock:((Double) -> Void)? = nil,
completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID? {
guard let phAsset = self.phAsset else { return nil }
var type: PHAssetResourceType? = nil
if phAsset.mediaSubtypes.contains(.photoLive) == true, convertLivePhotosToJPG == false {
Expand All @@ -165,6 +202,12 @@ public struct TLPHAsset {
}
}
guard let localURL = writeURL,let mimetype = MIMEType(writeURL) else { return nil }
if type == .pairedVideo {
return tempCopyLivePhotos(phAsset: phAsset,
livePhotoRequestOptions: livePhotoRequestOptions,
localURL: localURL,
completionBlock: { completionBlock(localURL, mimetype) })
}
switch phAsset.mediaType {
case .video:
var requestOptions = PHVideoRequestOptions()
Expand All @@ -179,7 +222,10 @@ public struct TLPHAsset {
progressBlock?(progress)
}
}
return PHImageManager.default().requestExportSession(forVideo: phAsset, options: requestOptions, exportPreset: exportPreset) { (session, infoDict) in
return PHImageManager.default().requestExportSession(forVideo: phAsset,
options: requestOptions,
exportPreset: exportPreset)
{ (session, infoDict) in
session?.outputURL = localURL
session?.outputFileType = AVFileType.mov
session?.exportAsynchronously(completionHandler: {
Expand All @@ -201,7 +247,9 @@ public struct TLPHAsset {
progressBlock?(progress)
}
}
return PHImageManager.default().requestImageData(for: phAsset, options: requestOptions, resultHandler: { (data, uti, orientation, info) in
return PHImageManager.default().requestImageData(for: phAsset,
options: requestOptions)
{ (data, uti, orientation, info) in
do {
var data = data
let needConvertLivePhotoToJPG = phAsset.mediaSubtypes.contains(.photoLive) == true && convertLivePhotosToJPG == true
Expand All @@ -213,7 +261,7 @@ public struct TLPHAsset {
completionBlock(localURL, mimetype)
}
}catch { }
})
}
default:
return nil
}
Expand Down

0 comments on commit e1ea38e

Please sign in to comment.