Skip to content

Commit

Permalink
started to implement iOS with Photos
Browse files Browse the repository at this point in the history
  • Loading branch information
viskin committed Sep 1, 2016
1 parent 7ab07ba commit 38c6ee4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 17 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
85 changes: 71 additions & 14 deletions src/ios/PhotoLibrary.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
import Photos

class ResultImage {
var id: String?
var title: String?
var width: Int?
var height: Int?
var data: String?
var thumbnail: String?
var creationDate: Int?
}

@objc(PhotoLibrary) class PhotoLibrary : CDVPlugin {


var fetchOptions: PHFetchOptions!

override func pluginInitialize() {
fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
if #available(iOS 9.0, *) {
fetchOptions.includeAssetSourceTypes = [.TypeUserLibrary, .TypeiTunesSynced, .TypeCloudShared]
}
}

// Will sort by creation date
func getPhotos(command: CDVInvokedUrlCommand) {
dispatch_async(dispatch_get_main_queue(), {

let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: self.fetchOptions)

var images = [ResultImage]()

fetchResult.enumerateObjectsUsingBlock {
(obj: AnyObject, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
let asset = obj as! PHAsset
let resultImage = ResultImage()
resultImage.id = asset.localIdentifier
resultImage.title = ""
resultImage.width = asset.pixelWidth
resultImage.height = asset.pixelHeight
resultImage.data = ""
resultImage.thumbnail = ""
resultImage.creationDate = 0
images.append(resultImage)
}

let pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK,
messageAsArray: images
)

self.commandDelegate!.sendPluginResult(
pluginResult,
callbackId: command.callbackId
)

});
}

func echo(command: CDVInvokedUrlCommand) {
var pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
Expand All @@ -9,30 +66,30 @@

if msg.characters.count > 0 {
/* UIAlertController is iOS 8 or newer only. */
let toastController: UIAlertController =
let toastController: UIAlertController =
UIAlertController(
title: "",
message: msg,
title: "",
message: msg,
preferredStyle: .Alert
)

self.viewController?.presentViewController(
toastController,
animated: true,
toastController,
animated: true,
completion: nil
)

let duration = Double(NSEC_PER_SEC) * 3.0

dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
DISPATCH_TIME_NOW,
Int64(duration)
),
dispatch_get_main_queue(),
{
),
dispatch_get_main_queue(),
{
toastController.dismissViewControllerAnimated(
true,
true,
completion: nil
)
}
Expand All @@ -45,8 +102,8 @@
}

self.commandDelegate!.sendPluginResult(
pluginResult,
pluginResult,
callbackId: command.callbackId
)
}
}
}
17 changes: 14 additions & 3 deletions www/PhotoLibrary.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
var exec = require('cordova/exec');

exports.echo = function(arg0, success, error) {
exports.echo = function (arg0, success, error) {
exec(success, error, "PhotoLibrary", "echo", [arg0]);
};

exports.echojs = function(arg0, success, error) {
if (arg0 && typeof(arg0) === 'string' && arg0.length > 0) {
exports.echojs = function (arg0, success, error) {
if (arg0 && typeof (arg0) === 'string' && arg0.length > 0) {
success(arg0);
} else {
error('Empty message!');
}

};

exports.getPhotos = function (success, error) {
cordova.exec(
success,
error,
'PhotoLibrary',
'getPhotos',
[]
);
}

0 comments on commit 38c6ee4

Please sign in to comment.