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: }
.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 = erroras 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://stackoverflow.com/questions/24101468/checking-optionals-for-nil-in-swift
Swift中获取相册图片与保存到相册的更多相关文章
- 修正iOS从照相机和相册中获取的图片 方向
修正iOS从照相机和相册中获取的图片 方向 修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...
- iOSQuartz2D-04-手动剪裁图片并保存到相册
实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一 ...
- Android中获取选择图片与获取拍照返回结果差异
导语: 如今的安卓应用在选择图片的处理上大多合并使用拍照和从相册中选择这两种方式 今天在写一个这样的功能时遇到一个尴尬的问题,同样是拍照获取图片功能,在不同手机上运行的效果不一样,下面是在某型手机上测 ...
- C# 从类库中获取资源图片,把图片资源保存到类库中
/// <summary> /// 获取资源图片 /// </summary> public class AssemblyHelper { #region 常量 /// < ...
- android把图片 视频 保存到相册
//android把图片文件添加到相册 ContentResolver localContentResolver = getContentResolver(); ContentValues local ...
- 修正iOS从照相机和相册中获取的图片方向(转)
- (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientati ...
- 修正iOS从照相机和相册中获取的图片方向
使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrientationLeft,在使用的时候会出现图片顺时针偏转90°.使用fixOrientati ...
- 系统相册中获取gif图片 保证取到的图片不会改变
NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL]; ...
- SWIFT中获取配置文件路径的方法
在项目中有时候要添加一些配置文件然后在程序中读取相应的配置信息,以下为本人整理的获取项目配置文件(.plist)路径的方法: 1.获取沙盒路径后再APPEND配置文件 func documentsDi ...
随机推荐
- How do I use EC2 Systems Manager to join an instance to my AWS Directory Service domain?
1. Create new role "EC2RoleforSSM" in AWS IAM AWS->IAM->Roles->Create role->Se ...
- nodejs与sqlite
//打开数据库var db = new sqlite3.Database('xx.db'); // 关闭数据库db.close(); db.run('xx'); // 数据库对象的run函数可以执行 ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- SQLAlchemy ORM教程之二:Query
from:https://www.jianshu.com/p/8d085e2f2657 这是继SQLAlchemy ORM教程之一:Create后的第二篇教程.在上一篇中我们主要是解决了如何配置ORM ...
- CSS变形
css3 变形/变换 相关属性 transform transform-origin transform-style:flat/preserve-3d perspective: 长度单位 perspe ...
- 自定义Java注解(annotation)
https://www.imooc.com/learn/456 笔记 Java从1.5开始引进注解. 首先解决一个问题,为什么要学习Java注解? 1.看懂别人写的代码,尤其是框架的代码 2.可以是 ...
- Linux管道符、重定向与环境变量
——<Linux就该这么学>笔记 输入输出重定向输入重定向 指把文件导入到命令中输出重定向 指把原本要输出到屏幕的数据信息写入到指定文件中 输出重定向 分为标准输出重定向和错误输出重定向 ...
- CSS中的HSLA颜色
CSS 中的颜色可以由RGB色彩空间和HSL色彩空间两种方式来表述.其中我们常用的是RGB色彩空间,RGB色彩空间的颜色表示方式有:十六进制颜色(如红色:#FF0000).RGB颜色(如红色:rgb( ...
- Selenium2+python自动化56-unittest之断言(assert)【转载】
前言 在测试用例中,执行完测试用例后,最后一步是判断测试结果是pass还是fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert). 用unittest组件测试用例的时候,断言 ...
- 用 python 来操作 docx, xlsx 格式文件(一)(使用 xlsxwriter 库操作xlsx格式文件)
需要从数据库读取日志生成相应的 docx,xlsx 文件做相应的记录 所以自然要用到docx, xlsxwriter 库 但是这些库的应用场景非常广泛,任何需要对 word,excel 文件执行重复性 ...