微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift 读取系统图片以及改变图片 圆形 UIImagePickerController

通过自己的摸索,终于弄出来了……

class SelectViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate {


var myActionSheet:UIActionSheet?

var picker:UIImagePickerController?

overridefunc viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

}

@IBOutletweak var img:UIImageView!


@IBOutletweak var btn_select:UIButton!

overridefunc didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// dispose of any resources that can be recreated.

}

//选取获取图片的方式

func openMenu()

{

myActionSheet =UIActionSheet(title: nil,delegate:self,cancelButtonTitle: "取消",destructiveButtonTitle:nil,otherButtonTitles:"打开相机","从相册选取")

myActionSheet?.showInView(self.view)

}

func actionSheet(actionSheet:UIActionSheet,clickedButtonAtIndex buttonIndex:Int) {

if(buttonIndex ==myActionSheet!.cancelButtonIndex)

{

NSLog("取消")

}

switch(buttonIndex)

{

case 1:takePhote()

break

case 2:localPhoto()

break

default :break

}

}

//拍照

func takePhote()

{

var sourceType :UIImagePickerControllerSourceType =UIImagePickerControllerSourceType.Camera

if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))

{

picker =UIImagePickerController()

picker?.delegate =self

picker!.allowsEditing =true

picker?.sourceType = sourceType

self.presentViewController(picker!,animated:true,completion: nil)

}

else{

NSLog("模拟器中无法打开照相机,请在真机上使用")

}

}

//选取当地的照片

func localPhoto()

{

picker =UIImagePickerController()

picker!.sourceType =UIImagePickerControllerSourceType.PhotoLibrary

picker!.allowsEditing =true

picker!.delegate =self

self.presentViewController(picker!,completion: nil)

}

//设置bar的颜色

func navigationController(navigationController:UINavigationController,willShowViewController viewController:UIViewController,animated: Bool) {

viewController.navigationController?.navigationBar.barTintColor =UIColor(red: 128/255,green: 167/255,blue: 223/255,alpha: 1)

viewController.navigationItem.rightBarButtonItem?.tintColor =UIColor.whiteColor()

}

//点击按钮时触发

@IBActionfunc onChange(sender: AnyObject) {

openMenu()

}

//选取图片之后

func imagePickerController(picker:UIImagePickerController,didFinishPickingMediawithInfo info: [NSObject :AnyObject]) {

var type :Nsstring = info["UIImagePickerControllerMediaType"]as Nsstring

if(type.isEqualToString("public.image"))

{

var image :UIImage = info["UIImagePickerControllerEditedImage"]as UIImage

redrwaimage(image,withParam: 5)

picker.dismissViewControllerAnimated(true,completion:nil)

}

}

//设置圆形图片

func redrwaimage(image:UIImage,withParam inset:CGFloat)

{

UIGraphicsBeginImageContext(image.size)

var context:CGContextRef =UIGraphicsGetCurrentContext()

CGContextSetlinewidth(context,2)

CGContextSetstrokeColorWithColor(context,UIColor.whiteColor().CGColor)

var rect:CGRect =CGRectMake(inset,inset,CGFloat(image.size.width- inset * 2),CGFloat(image.size.height- inset * 2))

CGContextAddEllipseInRect(context,rect)

CGContextClip(context)

image.drawInRect(rect)

CGContextAddEllipseInRect(context,rect)

CGContextstrokePath(context)

var newing =UIGraphicsGetimageFromCurrentimageContext()

UIGraphicsEndImageContext()

img.image = newing


}

//点击取消按钮时触发

func imagePickerControllerDidCancel(picker:UIImagePickerController) {

picker.dismissViewControllerAnimated(true,completion:nil)

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐