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) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |