copy地址:http://www.hangge.com/blog/cache/detail_902.html,这个网站不错,里面的东西挺实用,研究并自己敲一敲或者做成直接用的类是不错的选择.
这里是本人的地址,他那里的地址算是写死的,但是我这边不打算写死.所以加了拍照优化的功能,代码如下:(但是有一个BUG暂时无法解决,待解决了我再补充,大家自己用起来就知道在哪里了)
import UIKit
//从相册选取或者拍照需要实现UIImagePickerControllerDelegate UINavigationControllerDelegate
class ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
@IBOutlet weak var imageView: UIImageView!
var chooseImage: UIImage = UIImage()
//图片原图
// lazy var originalImage: UIImage = UIImage(named: "jobZ.png")!
//
// lazy var context: CIContext = CIContext(options: nil)
lazy var context: CIContext = {
return CIContext(options: nil)
}()
override func viewDidLoad() {
// self.imageView.image = UIImage(named: "jobZ.png")
}
//美化图片
@IBAction func autoAdjustImage(sender: UIButton) {
var inputImage = CIImage(image: self.chooseImage)
let options:[String : AnyObject] = [CIDetectorImageOrientation:1] //图片方向
let filters = inputImage!.autoAdjustmentFiltersWithOptions(options)
//遍历所有滤镜,依次处理图像
for filter: CIFilter in filters {
filter.setValue(inputImage, forKey: kCIInputImageKey)
inputImage = filter.outputImage
}
let cgImage = context.createCGImage(inputImage!, fromRect: inputImage!.extent)
self.imageView.image = UIImage(CGImage: cgImage)
}
//选取图片
@IBAction func chooseImage(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){
//初始化图片控制器
let picker = UIImagePickerController()
//设置代理
picker.delegate = self
//指定图片控制器类型 source我这里写的是用拍照,如果想用图库后面值改成UIImagePickerControllerSourceType.PhotoLibrary即可
picker.sourceType = UIImagePickerControllerSourceType.Camera
//如果有前置摄像头则调用前置摄像头
if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){
picker.cameraDevice = UIImagePickerControllerCameraDevice.Front
}
//弹出控制器,显示界面
self.presentViewController(picker, animated: true, completion: {
() -> Void in
})
}else{
print("读取相册错误")
}
}
//还原图片
@IBAction func resetImage(sender: UIButton) {
self.imageView.image = self.chooseImage
}
//代理
//选择图片成功后代理
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//查看info对象
print(info)
//获取选择的原图
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = image
self.chooseImage = image
//图片控制器退出
picker.dismissViewControllerAnimated(true, completion: {
() -> Void in
})
}
}
storyboard里的是如何设置的
然后大家试试效果吧 在文章顶部的链接里是有效果图的,建议那边去看看,我就不截图了