Swift中获取相册图片与保存到相册
关于这个网上目前位置记录的资料比较少,记录一下这个坑
获取相册图片
1: var iPC = UIImagePickerController()
2: iPC.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
3: iPC.delegate = self
4: presentViewController(iPC, animated: true) { () -> Void in
5: print("complete")
6: }
1: func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
2: // println(info)
3:
4: let img = info["UIImagePickerControllerOriginalImage"] as! UIImage
5: let pV = PhotoView(frame: paintView.frame, img: img)
6: view.addSubview(pV)
7: pV.delegate = self
8: photoView = pV
9: dismissViewControllerAnimated(true, completion: nil)
10: }
保存到系统相册
1: func save(){
2: UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
3: var ctx = UIGraphicsGetCurrentContext()
4: layer.renderInContext(ctx)
5: let img = UIGraphicsGetImageFromCurrentImageContext()
6: UIGraphicsEndImageContext()
7: UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)
8: }
1: func image(image: UIImage, didFinishSavingWithError error:NSErrorNSError?, contextInfo:UnsafePointer<Void>){
2: // if let r = error {
3: // print(r)
4: // }else{
5: // let e:NSError? = error
6: if let ee = erroras NSError?{
7: print(ee)
8: }else{
9: UIAlertView(title:nil, message: "保存成功!", delegate: nil, cancelButtonTitle: "确定").show()
10: }
11: }
以上代码在2015/7更新优化
Ref:
http://stackoverflow.com/questions/24101468/checking-optionals-for-nil-in-swift