对于iOS 中调用系统相册的功能,我想大家都比较熟悉了,但是Swift语言调用可能很多伙伴们不是很清楚,毕竟Swift是一门新语言,所以语法和实现方法可能不是很清楚,所以今天做了一个demo,大家可以做一下参考。
//
// ViewController.swift
// iOS
//
// Created by 悦兑科技 on 15/1/12.
// Copyright (c) 2015年 BSY. All rights reserved.
//
import UIKit
class ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
//创建UIButton
var button:UIButton = UIButton()
//设置frame
var frame = CGRectMake(100, 60, 100, 60)
button.frame = frame
//设置字体颜色
button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
//设置字体
button.setTitle("点我有惊喜", forState: UIControlState.Normal)
//添加方法
button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
//添加到父控件
self.view.addSubview(button)
}
/**
实现button方法
*/
func buttonClick(){
var pick:UIImagePickerController = UIImagePickerController()
pick.delegate = self
self.presentViewController(pick, animated: true, completion: nil)
}
/**
实现代理方法 UIImagePickerControllerDelegate,UINavigationControllerDelegate
*/
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
var imageview:UIImageView = UIImageView(frame: CGRectMake(0, 100, 320, 300))
let gotImage = info[UIImagePickerControllerOriginalImage] as UIImage
imageview.image = gotImage
self.view.addSubview(imageview)
println(info);
self.dismissViewControllerAnimated(true, completion: nil);
}
/**
实现代理方法 UIImagePickerControllerDelegate,UINavigationControllerDelegate
*/
func imagePickerControllerDidCancel(picker: UIImagePickerController){
}
}