swift--调用照相机和相册功能


//懒加载

lazy var imagePicker:ImagePickerViewController = ImagePickerViewController()

//按钮点击跳到相机

@IBAction func begin() {

presentViewController(imagePicker, animated: true, completion: nil)

}

//自定义(这是在另外一个文件中)

import UIKit

//继承自UIImagePickerController,遵守代理

class ImagePickerViewController: UIImagePickerController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

override func viewDidLoad() {

super.viewDidLoad()

// PhotoLibrary 相册(默认的)

// Camera 相机

// SavedPhotosAlbum已保存的相片

sourceType = UIImagePickerControllerSourceType.Camera//相机(只能在真机上使用)


self.view .addSubview(redView)

delegate = self

redView.backgroundColor = UIColor.redColor()

}

}


//拍照完了会调用

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

print("拍照完毕")

}

//选择照片会调用

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

print("选择相片")

}

//点击取消会调用

func imagePickerControllerDidCancel(picker: UIImagePickerController) {

print("取消")

}

由于楼主真机测试证书问题,暂时还没办法真机调试,后续调试后会更新,敬请期待...

你可能感兴趣的:(swift--调用照相机和相册功能)