Skip to content

Instantly share code, notes, and snippets.

@dddnuts
Last active May 6, 2016 14:42
Show Gist options
  • Save dddnuts/35c4c5f790034b4e34a9ac96bad2e607 to your computer and use it in GitHub Desktop.
Save dddnuts/35c4c5f790034b4e34a9ac96bad2e607 to your computer and use it in GitHub Desktop.
An Unity iOS Swift plugin to pick images from album
//
// DDDImageConverter.swift
// Unity-iPhone
//
// Created by dddnuts on 5/1/16.
//
//
import UIKit
import Photos
class DDDImageConverter: NSObject {
class func getImageURL(asset: PHAsset, size: CGSize) -> NSURL {
let manager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
options.synchronous = true
var publicURL = NSURL()
manager.requestImageForAsset(asset, targetSize: size, contentMode: .AspectFit, options: options, resultHandler: { result, info in
guard let image = result
else { return }
guard let imageData = UIImagePNGRepresentation(image)
else { return }
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd_HHmmss"
let dateString = dateFormatter.stringFromDate(NSDate())
let imageName = "dddimage_\(dateString).png"
let imagePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent(imageName)
let success = imageData.writeToFile(imagePath, atomically: true)
if !success {
return
}
publicURL = NSURL(fileURLWithPath: imagePath)
})
return publicURL
}
}
//
// DDDImagePicker.swift
// Unity-iPhone
//
// Created by dddnuts on 5/6/16.
//
//
import UIKit
import QBImagePicker
public class DDDImagePicker: NSObject {
public class func open() {
let picker = QBImagePickerController()
picker.delegate = DDDImagePickerDelegate.sharedInstance
picker.allowsMultipleSelection = true;
picker.minimumNumberOfSelection = 1;
picker.maximumNumberOfSelection = 1;
picker.assetCollectionSubtypes = [
PHAssetCollectionSubtype.SmartAlbumUserLibrary.rawValue,
PHAssetCollectionSubtype.AlbumMyPhotoStream.rawValue
];
let unityViewController = Unifty.GetGLViewController()
unityViewController.presentViewController(picker, animated: true, completion: nil)
}
}
//
// DDDImagePickerDelegate.swift
// Unity-iPhone
//
// Created by dddnuts on 5/6/16.
//
//
import UIKit
import QBImagePicker
class DDDImagePickerDelegate: NSObject, QBImagePickerControllerDelegate {
let DDDImagePickerDelegateObject = "ImagePicker"
let DDDImagePickerDelegateMethodComplete = "DidCompletePicking"
let DDDImagePickerDelegateMethodCancel = "DidCancelPicking"
static let sharedInstance = DDDImagePickerDelegate()
func qb_imagePickerControllerDidCancel(imagePickerController: QBImagePickerController) {
Unifty.SendMessage(DDDImagePickerDelegateObject,
method: DDDImagePickerDelegateMethodCancel,
msg: "")
let unityViewController = Unifty.GetGLViewController()
unityViewController.dismissViewControllerAnimated(true, completion: nil)
}
func qb_imagePickerController(imagePickerController: QBImagePickerController, didFinishPickingAssets assets: [AnyObject]) {
let image = assets.first as! PHAsset
let url = DDDImageConverter.getImageURL(image, size:CGSize(width: 320, height: 320))
Unifty.SendMessage(DDDImagePickerDelegateObject,
method: DDDImagePickerDelegateMethodComplete,
msg: url.absoluteString)
let unityViewController = Unifty.GetGLViewController()
unityViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
//
// DDDImagePickerPlugin.mm
// Unity-iPhone
//
// Created by dddnuts on 4/29/16.
//
//
#import <QBImagePicker/QBImagePicker.h>
#import <ProductName-Swift.h>
extern "C" {
void DDDImagePicker_Open() {
[DDDImagePicker open];
}
}
SWIFT_CLASS("_TtC9ProductName14DDDImagePicker")
@interface DDDImagePicker : NSObject
+ (void)open;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment