UIImagePickerController与IOS9的iPad

由于iOS 9和Xcode 7,我不再能够在iPad上实现UIImagePickerController(包括设备和模拟器)。 下面的代码在iPad上工作,但仅在iOS 9之前。当使用iOS 9 +时,呈现的图像(UIImagePickerController被解除后)是所选图像的不正确版本。 没有重新resize或裁剪,最终图像只是原始图像的右上angular? 再加上另外一个问题 – 如果imagePicker.allowsEditing = false,则无法从PhotoLibrary中select图像?

@IBAction func photoButton(sender: AnyObject) { imagePicker.allowsEditing = true imagePicker.sourceType = .PhotoLibrary self.presentViewController(imagePicker, animated: false, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage { self.imageView.image = pickedImage dismissViewControllerAnimated(true, completion: { () -> Void in }) } 

下面是在UIImagePickerController中显示的选定图像的示例。 (请注意,所选图像的显示效果非常小,而不是像以前那样显示为全屏大小/宽度)

在这里输入图像说明

在UIImagePickerController中select使用button后,最终的图像只是原始图像的右上angular。 我在做什么错或UIImagePickerController在iOS 9上打破?

在这里输入图像说明

这是来自Apple的一个错误: http : //openradar.appspot.com/radar?id=5032957332946944

目前糟糕的解决方法:

  if UIDevice.currentDevice().userInterfaceIdiom == .Pad { imagePicker.allowsEditing = false } else { imagePicker.allowsEditing = true } 

Swift 3.0:

 if UIDevice.current.userInterfaceIdiom == .pad { } 

参考下面的编码

 @IBAction func photoButton(sender: AnyObject) { if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { picker!.sourceType = UIImagePickerControllerSourceType.Camera self .presentViewController(picker!, animated: true, completion: nil) } else { let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"") alertWarning.show() } } func openGallary() { picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(picker!, animated: true, completion: nil) } //PickerView Delegate Methods func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { picker .dismissViewControllerAnimated(true, completion: nil) imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage } func imagePickerControllerDidCancel(picker: UIImagePickerController) { println("picker cancel.") } 

您需要确保您的imageview具有正确的框架,并且imageview的contentmode应该是aspectfit,而select器返回的图像types是[UIImagePickerControllerOriginalImage]

 [imageView setFrame:imageViewRect]; [imageView setContentMode:UIViewContentModeScaleAspectFit]; 

迅速:

  imageView.setFrame(imageViewRect) imageView.setContentMode(UIViewContentModeScaleAspectFit) 

并来到了allowsEditing属性,这与照片select无关。

它是一个布尔值,指示用户是否被允许编辑选定的静止图像或电影。

如果要从相机库中select照片,则需要将源types修改为

  picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

更新:

LEts说你已经select了一个图像并显示在图像视图上。 我假设你在视图上有一个图像视图,并且图像视图帧等于视图帧。 如果IB是自由格式的,我假设IB中imageview框架的大小为600×600。

如果你只是做:

  _startImageView.image=_img; 

结果将是:

在这里输入图像说明

现在,让我们对imageview中显示的图像进行一些更改:

  CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(_img.size, CGRectMake(0, 0, self.startImageView.frame.size.width, self.startImageView.frame.size.height)); UIGraphicsBeginImageContext(CGSizeMake(_startImageView.frame.size.width,_startImageView.frame.size.height)); [_img drawInRect:scaledRect]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); _startImageView.image=scaledImage; 

现在形象将是:

在这里输入图像说明

原始图像select大小为640×426, 当不缩放

缩放到最大时,select的原始图像尺寸为1536×1286 (使用两指缩放操作)

正如你所看到的,图像没有大的变化, 这是因为imageview在图像收到图像时已经被裁剪/缩放了

所以即使你尝试去做:

  [_img drawInRect:_startImageView.frame]; 

图像将不会按照我们的需要绘制,因为图像已经按比例缩放,因为拾取器给出的图像是edited image

解:

要解决这个问题,你需要从didFinishPickingMediaWithInfo:方法中的select器中select原始图像

  info[UIImagePickerControllerOriginalImage]; 

这给你的形象:

在这里输入图像说明

这个代码和@ pkc456比较相似,只是稍微短一些。

这对我来说是完美的:

  import UIKit class PostImageViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet var imageToPost: UIImageView! @IBAction func chooseImage(sender: AnyObject) { var image = UIImagePickerController() image.delegate = self image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary image.allowsEditing = false //or true additional setup required. self.presentViewController(image, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { self.dismissViewControllerAnimated(true, completion:nil) } 

这是否工作?

我在xcode 7.1(Swift)上工作,发现你的代码是合适的。 我也在我的项目上写下了下面的代码,它正在成功的工作。

 func showPicker(){ let type = UIImagePickerControllerSourceType.PhotoLibrary if(UIImagePickerController.isSourceTypeAvailable(type)){ let pickerController = UIImagePickerController() pickerController.delegate = self pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary pickerController.allowsEditing = false self.presentViewController(pickerController, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { let imageView = self.view.viewWithTag(20) as! UIImageView let selectedImage : UIImage = image imageView.image=selectedImage self.dismissViewControllerAnimated(true, completion: nil) } 

您的代码和我的代码唯一的区别是关于ImagePickerController实例的可见性。 供您参考,我上传我的代码: – https://www.dropbox.com/s/pe0yikxsab8u848/ImagePicker-Swift.zip?dl=0

我的想法是看我的代码一次,可能你会得到你的代码故障的哪一部分的想法。

看来你正在使用IB和自动布局,但设置了正确的约束。 尝试在故事板中添加一些限制。

对我来说,我解决了,显示模式为popover

  @IBAction func photoButton(sender: AnyObject) { imagePicker.allowsEditing = true imagePicker.sourceType = .PhotoLibrary let controller = self.imagePicker controller.modalPresentationStyle = UIModalPresentationStyle.Popover controller.modalTransitionStyle = UIModalTransitionStyle.CoverVertical let popover = controller.popoverPresentationController popover?.sourceView = self controller.preferredContentSize = CGSize( width: self.frame.width * 0.6, height: self.frame.height * 0.6 ) popover?.sourceRect = CGRectMake( CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), 0, 0 ) self.presentViewController(self.imagePicker, animated: true, completion: nil) }