码迷,mamicode.com
首页 > 编程语言 > 详细

Swift中获取相册图片与保存到相册

时间:2015-05-19 08:47:37      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

关于这个网上目前位置记录的资料比较少,记录一下这个坑

获取相册图片

   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: NSError, contextInfo:UnsafePointer<Void>){
   2:  //        if let r = error {
   3:  //            print(r)
   4:  //        }else{
   5:  //        let e:NSError? = error
   6:          if let ee = error as NSError? {
   7:              print(ee)
   8:          }else{
   9:              UIAlertView(title:nil, message: "保存成功!", delegate: nil, cancelButtonTitle: "确定").show()
  10:          }
  11:      }

 

 

Ref:

http://stackoverflow.com/questions/25108888/strugglig-to-convert-objective-c-selector-and-target-signature-to-swift

http://stackoverflow.com/questions/24101468/checking-optionals-for-nil-in-swift

Swift中获取相册图片与保存到相册

标签:

原文地址:http://www.cnblogs.com/zeyang/p/4513534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!