IOS SWIFT alte形式获取手机的相册和相机
2015-10-28 14:47
375 查看
import UIKit
class ViewController:
UIViewController,UIImagePickerControllerDelegate,
UINavigationControllerDelegate{
let imagePickerController:
UIImagePickerController = UIImagePickerController()
var isFullScreen:
Bool = false
var imageData=NSData()
var img = UIImage()
@IBOutlet
weak var image:
UIImageView!
@IBAction func btnClicked(sender:
UIButton) {
// 设置代理
self.imagePickerController.delegate =
self
// 设置是否可以管理已经存在的图片或者视频
self.imagePickerController.allowsEditing =
true
//
判断是否支持相机
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let alertController:
UIAlertController = UIAlertController(title:
nil, message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)
let cameraAction:
UIAlertAction = UIAlertAction(title:
"拍照换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.Camera
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(cameraAction)
let photoLibraryAction:
UIAlertAction = UIAlertAction(title:
"从相册选择换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor =
UIColor(red: 171/255, green:
202/255, blue:
41/255, alpha:
1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes
= [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor
= UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction:
UIAlertAction = UIAlertAction(title:
"取消", style: .Cancel, handler:
nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated:
true, completion:
nil)
}else{
let alertController:
UIAlertController = UIAlertController(title:
nil, message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)
let photoLibraryAction:
UIAlertAction = UIAlertAction(title:
"从相册选择换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor =
UIColor(red: 171/255, green:
202/255, blue:
41/255, alpha:
1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes
= [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor
= UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction:
UIAlertAction = UIAlertAction(title:
"取消", style: .Cancel, handler:
nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated:
true, completion:
nil)
}
}
func imagePickerController(picker:
UIImagePickerController, didFinishPickingMediaWithInfo info: [String :
AnyObject]) {
picker.dismissViewControllerAnimated(true, completion:
nil)
img = info[UIImagePickerControllerEditedImage]
as!
UIImage
image.image =
img
imageData=UIImageJPEGRepresentation(img,
1)!
//保存文件
var path:Array =
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)
let FilePath=path[0].stringByAppendingString("/HomeImage.png")
imageData.writeToFile(FilePath, atomically:
true)
picker.dismissViewControllerAnimated(true, completion:
nil)
}
// 当用户取消时,调用该方法
func imagePickerControllerDidCancel(picker:
UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion:
nil)
}
}
class ViewController:
UIViewController,UIImagePickerControllerDelegate,
UINavigationControllerDelegate{
let imagePickerController:
UIImagePickerController = UIImagePickerController()
var isFullScreen:
Bool = false
var imageData=NSData()
var img = UIImage()
@IBOutlet
weak var image:
UIImageView!
@IBAction func btnClicked(sender:
UIButton) {
// 设置代理
self.imagePickerController.delegate =
self
// 设置是否可以管理已经存在的图片或者视频
self.imagePickerController.allowsEditing =
true
//
判断是否支持相机
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let alertController:
UIAlertController = UIAlertController(title:
nil, message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)
let cameraAction:
UIAlertAction = UIAlertAction(title:
"拍照换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.Camera
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(cameraAction)
let photoLibraryAction:
UIAlertAction = UIAlertAction(title:
"从相册选择换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor =
UIColor(red: 171/255, green:
202/255, blue:
41/255, alpha:
1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes
= [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor
= UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction:
UIAlertAction = UIAlertAction(title:
"取消", style: .Cancel, handler:
nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated:
true, completion:
nil)
}else{
let alertController:
UIAlertController = UIAlertController(title:
nil, message: nil, preferredStyle:
UIAlertControllerStyle.ActionSheet)
let photoLibraryAction:
UIAlertAction = UIAlertAction(title:
"从相册选择换头像", style: .Default) { (action:
UIAlertAction!) ->
Void in
// 设置类型
self.imagePickerController.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
//改navigationBar背景色
self.imagePickerController.navigationBar.barTintColor =
UIColor(red: 171/255, green:
202/255, blue:
41/255, alpha:
1.0)
//改navigationBar标题色
self.imagePickerController.navigationBar.titleTextAttributes
= [NSForegroundColorAttributeName: UIColor.whiteColor()]
//改navigationBar的button字体色
self.imagePickerController.navigationBar.tintColor
= UIColor.whiteColor()
self.presentViewController(self.imagePickerController, animated:
true, completion:
nil)
}
alertController.addAction(photoLibraryAction)
let cancelAction:
UIAlertAction = UIAlertAction(title:
"取消", style: .Cancel, handler:
nil)
alertController.addAction(cancelAction)
presentViewController(alertController, animated:
true, completion:
nil)
}
}
func imagePickerController(picker:
UIImagePickerController, didFinishPickingMediaWithInfo info: [String :
AnyObject]) {
picker.dismissViewControllerAnimated(true, completion:
nil)
img = info[UIImagePickerControllerEditedImage]
as!
UIImage
image.image =
img
imageData=UIImageJPEGRepresentation(img,
1)!
//保存文件
var path:Array =
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)
let FilePath=path[0].stringByAppendingString("/HomeImage.png")
imageData.writeToFile(FilePath, atomically:
true)
picker.dismissViewControllerAnimated(true, completion:
nil)
}
// 当用户取消时,调用该方法
func imagePickerControllerDidCancel(picker:
UIImagePickerController) {
self.dismissViewControllerAnimated(true, completion:
nil)
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift中定义二维数组的方法及遍历方法示例
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例