Skip to content
Snippets Groups Projects
Commit 0036c152 authored by viskin's avatar viskin
Browse files

added image2DataURL

parent 4ba79f4e
No related branches found
No related tags found
No related merge requests found
Parts are based on
- https://github.com/ryouaki/Cordova-Plugin-Photos
- https://github.com/subitolabs/cordova-gallery-api
......@@ -39,6 +39,30 @@ import Foundation
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
}
private func image2DataURL(image: UIImage) -> String? {
var imageData: NSData?
var mimeType: String
if (imageHasAlpha(image)){
imageData = UIImagePNGRepresentation(image)
mimeType = "image/png"
} else {
imageData = UIImageJPEGRepresentation(image, 1.0)
mimeType = "image/jpeg"
}
if (imageData != nil) {
let encodedString = imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
if (encodedString != nil) {
return String(format: "data:%@;base64,%@", mimeType, encodedString!)
}
}
return nil
}
private func imageHasAlpha(image: UIImage) -> Bool {
let alphaInfo = CGImageGetAlphaInfo(image.CGImage)
return alphaInfo == .First || alphaInfo == .Last || alphaInfo == .PremultipliedFirst || alphaInfo == .PremultipliedLast
}
// Will sort by creation date
func getPhotos(command: CDVInvokedUrlCommand) {
dispatch_async(dispatch_get_main_queue(), {
......@@ -66,7 +90,7 @@ import Foundation
resultImage["id"] = asset.localIdentifier
resultImage["filename"] = imageURL?.pathComponents?.last
resultImage["nativeUrl"] = imageURL?.absoluteString //TODO: in Swift 3, use JSONRepresentable
resultImage["url"] = nil //TODO: convert to data url
resultImage["url"] = image != nil ? self.image2DataURL(image!) : nil
resultImage["width"] = asset.pixelWidth
resultImage["height"] = asset.pixelHeight
resultImage["creationDate"] = self.dateFormatter.stringFromDate(asset.creationDate!) //TODO: in Swift 3, use JSONRepresentable
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment