swift3.0调用相机和相册 简单实用

前端之家收集整理的这篇文章主要介绍了swift3.0调用相机和相册 简单实用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、首先,swift3.0中调用相机和相册会导致崩溃,需要在info.plist文件中加入两个键值对,如下:

Privacy - Photo Library Usage Description 和Privacy - Camera Usage Description ,都是String类型,内容任意的字符串即可。

2、废话少说,上代码

@H_502_16@class MyController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

var uploadAlertController:UIAlertController!

@H_502_16@var imagePickerController:UIImagePickerController!

@H_502_16@@IBOutlet@H_502_16@weak@H_502_16@var headImg:UIImageView!


@H_502_16@override@H_502_16@func viewDidLoad() {

@H_502_16@super.viewDidLoad()

@H_502_16@self.initAlertController()

@H_502_16@self.initImagePickerController()

}

func initAlertController()

{

@H_502_16@weak@H_502_16@var blockSelf =@H_502_16@self

@H_502_16@self.uploadAlertController =UIAlertController(title:@H_502_16@nil,message: @H_502_16@nil,preferredStyle:UIAlertControllerStyle.actionSheet)

@H_502_16@self.uploadAlertController.view.tintColor = DeepMainColor

@H_502_16@let takePhoto =UIAlertAction(title:"拍照",style:UIAlertActionStyle.default) { (action:UIAlertAction)@H_502_16@in

blockSelf?.actionAction(action: action)

}

@H_502_16@let photoLib =UIAlertAction(title:"从相册选择",style:UIAlertActionStyle.default) { (action:UIAlertAction)@H_502_16@in

blockSelf?.actionAction(action: action)

}

@H_502_16@let cancel =UIAlertAction(title:"取消",style:UIAlertActionStyle.cancel) { (action:UIAlertAction)@H_502_16@in

blockSelf?.actionAction(action: action)

}

@H_502_16@self.uploadAlertController?.addAction(takePhoto)

@H_502_16@self.uploadAlertController?.addAction(photoLib)

@H_502_16@self.uploadAlertController?.addAction(cancel)

}

@H_502_16@func initImagePickerController()

{

@H_502_16@self.imagePickerController =UIImagePickerController()

@H_502_16@self.imagePickerController.delegate = @H_502_16@self

// 设置是否可以管理已经存在的图片或者视频

@H_502_16@self.imagePickerController.allowsEditing = @H_502_16@true

}

@H_502_16@func actionAction(action:UIAlertAction)

{

@H_502_16@if action.title =="拍照" {

@H_502_16@self.getImageFromPhotoLib(type: .camera)

}@H_502_16@else@H_502_16@if action.title =="从相册选择"|| action.title =="更换头像" {

@H_502_16@self.getImageFromPhotoLib(type: .photoLibrary)

}@H_502_16@else@H_502_16@if action.title =="删除照片" {

@H_502_16@self.headImg.image =UIImage(named:"head")

}

}

@H_502_16@func getImageFromPhotoLib(type:UIImagePickerControllerSourceType)

{

@H_502_16@self.imagePickerController.sourceType = type

//判断是否支持相册

@H_502_16@ifUIImagePickerController.isSourceTypeAvailable(.photoLibrary) {

@H_502_16@self.present(@H_502_16@self.imagePickerController,animated: @H_502_16@true,completion:@H_502_16@nil)

}

}

//MARK:- UIImagePickerControllerDelegate

@H_502_16@func imagePickerController(@H_502_16@_ picker:UIImagePickerController,didFinishPickingMediaWithInfo info: [String :@H_502_16@Any]){

@H_502_16@let type:String = (info[UIImagePickerControllerMediaType]@H_502_16@as!String)

//当选择的类型是图片

@H_502_16@if type=="public.image"

{

@H_502_16@let img = info[UIImagePickerControllerOriginalImage]@H_502_16@as?UIImage

@H_502_16@self.headImg.image =cropToBounds(image: img!)

@H_502_16@let imgData =UIImageJPEGRepresentation(@H_502_16@self.headImg.image!,0.5)

picker.dismiss(animated:@H_502_16@true,completion:@H_502_16@nil)

}

}

func imagePickerControllerDidCancel(@H_502_16@_ picker:UIImagePickerController){

picker.dismiss(animated:@H_502_16@true,completion:@H_502_16@nil)

}

@IBAction@H_502_16@func headImgTapGesture(@H_502_16@_ sender:AnyObject) {

present(@H_502_16@self.uploadAlertController,animated:@H_502_16@true,completion: @H_502_16@nil)

}

}

原文链接:https://www.f2er.com/swift/322980.html

猜你在找的Swift相关文章