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

时间:2024-10-11 09:08:14

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

获取相册图片

   1:  var iPC = UIImagePickerController()
   2:          iPC.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
   3:          iPC.delegate = self
   4:          presentViewController(iPC, animated: true) { () -> Void in
   5:              print("complete")
   6:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

   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:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

保存到系统相册

   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:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

   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:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

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

 

 

Ref:

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

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