我有一个tableview,显示具有imageview和标签的人.当一个人插入桌子时,它可以从触点拉出,图像看起来很好.如果未使用联系人添加某个人,则会从文本字段中提取名称,并为相机显示图像选择器.如果我从后置摄像头拍照没有问题.如果我从前置摄像头拍摄照片,则根据屏幕方向关闭照片.即使在拍摄照片时屏幕没有旋转,它也会根据以下情况关闭:
如果方向为纵向,则图像旋转180度.如果方向是横向左侧,则图像向左旋转90度.如果方向是颠倒的,则图像向右旋转90度.如果方向是正确的,图像就可以了.
这是我提供图像选择器的代码:
func pickImage(){
// create the alert controller, give it three actions: library, camera, cancel
let pickerAlert = UIAlertController(title: "Add Pic?", message: "Would you like to add a picture for this person?", preferredStyle: .Alert)
// create the library action
let libAction = UIAlertAction(title: "Select photo from library", style: .Default) { (alert) -> Void in
// set picker type to the library and present the picker
self.picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(self.picker, animated: true, completion: nil)
}
// if the camera is available on the device create the camera action and add it to the picker alert
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
let camAction = UIAlertAction(title: "Take a picture", style: .Default) { (alert) -> Void in
// set picker type to the camera and present the picker
self.picker.sourceType = UIImagePickerControllerSourceType.Camera
self.picker.modalPresentationStyle = .FullScreen
self.presentViewController(self.picker, animated: true, completion: nil)
}
pickerAlert.addAction(camAction)
}
// create the cancel action, set the person's image to contacts
let cancel = UIAlertAction(title: "Don't Add Image", style: .Cancel, handler: nil)
pickerAlert.addAction(libAction)
pickerAlert.addAction(cancel)
self.presentViewController(pickerAlert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
这是我的imagePicker委托:(不包括didCancel,它只是解雇了选择器)
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let imgData = UIImagePNGRepresentation(image)
saveToList(personName, imgData: imgData!)
self.tableView.reloadData()
self.dismissViewControllerAnimated(true, completion: nil)}
Run Code Online (Sandbox Code Playgroud)
当然,我不能将图像转换为PNG数据?这些方法也非常基础.我对选择器没有太多了解.保存到列表方法只需获取名称和图像数据,并将其存储在我的数据模型中.当我在单元格中检索图像时,我使用人物的图像数据设置了图像视图图像.我已阅读Apple文档中的图像选择器,似乎无法找到我遗漏的东西:-(
换掉UIImagePNGRepresentation并UIImageJPEGRepresentation改用.PNG不会自动保存方向信息.除非您正在制作照片质量必须完美的照片专用应用程序,否则JPG很好.
该UIImageJPEGRepresentation会要求的图像数据,但也CGFloat设置压缩质量.它从0.0到1.0(最好).
祝好运!
| 归档时间: |
|
| 查看次数: |
1194 次 |
| 最近记录: |