Skip to content

Commit

Permalink
support : convert heic to png
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Mar 19, 2018
1 parent ef2e681 commit b183f3b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Example/TLPhotoPicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ViewController: UIViewController,TLPhotosPickerViewControllerDelegate {

func getAsyncCopyTemporaryFile() {
if let asset = self.selectedAssets.first {
asset.tempCopyMediaFile(progressBlock: { (progress) in
asset.tempCopyMediaFile(convertLivePhotosToPNG: false, progressBlock: { (progress) in
print(progress)
}, completionBlock: { (url, mimeType) in
print(mimeType)
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ public struct TLPHAsset {
@discardableResult
public func cloudImageDownload(progressBlock: @escaping (Double) -> Void, completionBlock:@escaping (UIImage?)-> Void ) -> PHImageRequestID?
// get original media file async copy temporary media file ( photo(png,gif...etc.) and video ) -> Don't forget, You should delete temporary file.
public func tempCopyMediaFile(progressBlock:((Double) -> Void)? = nil, completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID?
// parmeter : convertLivePhotosToPNG
// false : If you want mov file at live photos
// true : If you want png file at live photos ( HEIC )
public func tempCopyMediaFile(convertLivePhotosToPNG: Bool = false, progressBlock:((Double) -> Void)? = nil, completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID?
// get original asset file name
public var originalFileName: String?
}
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.5.3'
s.version = '1.5.4'
s.summary = 'multiple phassets picker for iOS lib. like facebook'

# This description is used to generate tags and improve search results.
Expand Down
17 changes: 15 additions & 2 deletions TLPhotoPicker/Classes/TLAssetsCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ public struct TLPHAsset {
}

@discardableResult
public func tempCopyMediaFile(progressBlock:((Double) -> Void)? = nil, completionBlock:@escaping ((URL,String) -> Void)) -> PHImageRequestID? {
//convertLivePhotosToPNG
// false : If you want mov file at live photos
// true : If you want png file at live photos ( HEIC )
public func tempCopyMediaFile(convertLivePhotosToPNG: 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 {
if phAsset.mediaSubtypes.contains(.photoLive) == true, convertLivePhotosToPNG == false {
type = .pairedVideo
}else {
type = phAsset.mediaType == .video ? .video : .photo
Expand All @@ -153,6 +156,12 @@ public struct TLPHAsset {
} else {
writeURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("\(fileName)")
}
if (writeURL?.pathExtension.uppercased() == "HEIC" || writeURL?.pathExtension.uppercased() == "HEIF") && convertLivePhotosToPNG {
if let fileName2 = writeURL?.deletingPathExtension().lastPathComponent {
writeURL?.deleteLastPathComponent()
writeURL?.appendPathComponent("\(fileName2).png")
}
}
guard let localURL = writeURL,let mimetype = MIMEType(writeURL) else { return nil }
switch phAsset.mediaType {
case .video:
Expand Down Expand Up @@ -182,6 +191,10 @@ public struct TLPHAsset {
}
return PHImageManager.default().requestImageData(for: phAsset, options: options, resultHandler: { (data, uti, orientation, info) in
do {
var data = data
if convertLivePhotosToPNG == true, let imgData = data, let rawImage = UIImage(data: imgData) {
data = UIImagePNGRepresentation(rawImage)
}
try data?.write(to: localURL)
DispatchQueue.main.async {
completionBlock(localURL, mimetype)
Expand Down

0 comments on commit b183f3b

Please sign in to comment.