ios中View.swift和Controller.swift添加上传图片的控件
两者的区别是每个函数的最后一句话
View.swift中添加控件alertController
func addcerclick() {
//alertController定义为列表形式(ActionSheet)的弹出
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
//下面定义了三行
let albumAction = UIAlertAction(title: "相册", style: .Default) { (action) -> Void in
self.pickPicture()//闭包实现点击时调用
}
let photoAction = UIAlertAction(title: "拍照", style: .Default) { (action) -> Void in
self.takePicture()
}
//类型为style: .Cancel,
let defaultAction = UIAlertAction(title: LocalizationManager.getStringByKey("Cancel"), style: .Cancel, handler: nil)
alertController.addAction(albumAction)
alertController.addAction(photoAction)
alertController.addAction(defaultAction)
//var pushVCClick : ((flag:String, vc:UIViewController) -> ())?
//闭包实现本页面的遮罩,Controller中不能使用该方法
self.pushVCClick!(flag: "picture",vc: alertController)
}
// 拍照
func takePicture()
{
//UIImagePickerControllerSourceType.Camera代表的是手机相机
let sourceType = UIImagePickerControllerSourceType.Camera
//如果相机可用
if UIImagePickerController.isSourceTypeAvailable(sourceType)
{
//picker代表相机空间
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = sourceType
self.pushVCClick!(flag: "picture",vc: picker)
}else
{
self.pushVCClick!(flag: "没有摄像头",vc: UIViewController())
}
}
// 选择图片
func pickPicture()
{
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.pushVCClick!(flag: "picture",vc: picker)
}
// 图像选择后,系统的回调方法
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let type: AnyObject? = info["UIImagePickerControllerMediaType"]
// 照片
if type!.isEqualToString("public.image")
{
var image = info["UIImagePickerControllerEditedImage"] as! UIImage
if UIImageJPEGRepresentation(image, 1) != nil
{
image = UtilityFromOC.fixOrientation(image)
var nsdataforimage = UIImageJPEGRepresentation(image, 1)!
//将图片压缩
while nsdataforimage.length/1024 > 100{
nsdataforimage = UIImageJPEGRepresentation(image,0.1)!;
// 压缩比例在0~1之间
}
//NSData 类型转 String 类型
let str:String = nsdataforimage.base64EncodedStringWithOptions(.Encoding64CharacterLineLength )
// String 类型转NSData 类型
// let nsdataforimage1 : NSData = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
image = UIImage(data: nsdataforimage)!
//展示图片到页面,var picimg = UIImage(named: "add_cer_icon")
picimg = image
tableView.reloadData()
}
}
// 这一步之后将调用view did appear事件
picker.dismissViewControllerAnimated(true, completion: nil)
}
Controller.swift中添加控件alertController
func addcerclick() {
//alertController定义为列表形式(ActionSheet)的弹出
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
//下面定义了三行
let albumAction = UIAlertAction(title: "相册", style: .Default) { (action) -> Void in
self.pickPicture()//闭包实现点击时调用
}
let photoAction = UIAlertAction(title: "拍照", style: .Default) { (action) -> Void in
self.takePicture()
}
//类型为style: .Cancel,
let defaultAction = UIAlertAction(title: LocalizationManager.getStringByKey("Cancel"), style: .Cancel, handler: nil)
alertController.addAction(albumAction)
alertController.addAction(photoAction)
alertController.addAction(defaultAction)
print("进入遮罩")
//实现本页面的遮罩
self.presentViewController(alertController, animated: true, completion: nil)
}
// 拍照
func takePicture()
{
//UIImagePickerControllerSourceType.Camera代表的是手机相机
let sourceType = UIImagePickerControllerSourceType.Camera
//如果相机可用
if UIImagePickerController.isSourceTypeAvailable(sourceType)
{
//picker代表相机空间
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = sourceType
//self.pushVCClick!(flag: "picture",vc: picker)
self.presentViewController(picker, animated: true, completion: nil)
}else
{
//self.pushVCClick!(flag: "没有摄像头",vc: UIViewController())
SimpleAlert("没有摄像头", container: self)
}
}
// 选择图片
func pickPicture()
{
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
//self.pushVCClick!(flag: "picture",vc: picker)
self.presentViewController(picker, animated: true, completion: nil)
}
// 图像选择后,系统的回调方法
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let type: AnyObject? = info["UIImagePickerControllerMediaType"]
// 照片
if type!.isEqualToString("public.image")
{
var image = info["UIImagePickerControllerEditedImage"] as! UIImage
if UIImageJPEGRepresentation(image, 0.2) != nil
{
image = UtilityFromOC.fixOrientation(image)
let nsdataforimage = UIImageJPEGRepresentation(image, 0.2)!
image = UIImage(data: nsdataforimage)!
saveAvatarImage(image,filename: "user_avatar")
//展示图片到页面,var picimg = UIImage(named: "add_cer_icon")
picimg = image
//添加后的重新加载,
headImageView.image=picimg
}
}
// 这一步之后将调用view did appear事件
picker.dismissViewControllerAnimated(true, completion: nil)
}

浙公网安备 33010602011771号