繁体   English   中英

带有IOS9的iPad上的UIImagePickerController

[英]UIImagePickerController on iPad with IOS9

从iOS 9和Xcode 7开始,我无法再在iPad(设备和模拟器)上实现UIImagePickerController。 以下代码适用于iPad,但仅适用于iOS 9.使用iOS 9+时,显示的图像(在UIImagePickerController被取消后)是所选图像的错误版本。 没有重新调整大小或裁剪最终图像只是原始图像的右上角?? 另外一个问题 - 如果imagePicker.allowsEditing = false,你无法从PhotoLibrary中选择图像?

@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中选择使用按钮后,最终图像仅在原始图像的右上角。 我在做错了什么或是在iOS 9上破坏了UIImagePickerController?

在此输入图像描述

这是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的内容模式应该是aspectfit,而选择器返回的图像是[UIImagePickerControllerOriginalImage]类型

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

迅速:

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

并且来到allowsEditing属性,它与照片选择无关。

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

如果要从相机库中选择照片,则需要将源类型修改为

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

更新:

LEts表示您已选择一个图像并将其显示在图像视图上。 我假设您在视图上有一个imageview,并且imageview框架等于视图框架。 如果IB是自由形式的,我假设IB中的imageview帧的大小为600x600。

如果你只是这样做:

     _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;

现在图像将是:

在此输入图像描述

选择的原始图像大小为640x426, 未缩放时

缩放到最大时选择的原始图像大小为1536x1286 (使用两个手指缩放动作)

正如您所看到的,图像中仍然没有太大的变化, 这是因为在您的imageview收到图像时图像已被裁剪/缩放

所以即使你试着这样做:

      [_img drawInRect:_startImageView.frame];

根据我们的需要不会绘制图像,因为图像已经在它们的右侧缩放,因为拾取器给出的图像是edited image

解:

要解决此问题,您需要在didFinishPickingMediaWithInfo:方法中从选择器中选择原始图像

       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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM