• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

获取相册图片

   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 = error as NSError? {
   7:              print(ee)
   8:          }else{
   9:              UIAlertView(title:nil, message: "保存成功!", delegate: nil, cancelButtonTitle: "确定").show()
  10:          }
  11:      }

以上代码在2015/7更新优化

 

 

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap