写真を複数枚選択するのは標準に無いので、ライブラリを使う。
ELCImagePickerController
GitHub: https://github.com/B-Sides/ELCImagePickerController
コード
import UIKit
class ViewController: UIViewController, ELCImagePickerControllerDelegate {
@IBAction func addButton(sender: AnyObject) {
self.pickImages()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 写真を選択する
func pickImages() {
let picker = ELCImagePickerController()
picker.maximumImagesCount = 5 // 選択する最大枚数
picker.imagePickerDelegate = self
self.presentViewController(picker, animated: true, completion: nil)
}
// 写真選択時に呼び出される
func elcImagePickerController(picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [AnyObject]!) {
self.dismissViewControllerAnimated(true, completion: nil)
if (info.count == 0) {
return
}
var pickedImages = NSMutableArray()
for any in info {
let dict = any as NSMutableDictionary
let image = dict.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
pickedImages.addObject(image)
}
println(pickedImages)
}
// 写真未選択時に呼び出される
func elcImagePickerControllerDidCancel(picker: ELCImagePickerController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
٩(๑❛ᴗ❛๑)۶