UIImagePickerController on iPad with IOS9

人走茶凉 提交于 2019-12-02 17:57:53
MPaulo

This is a bug from Apple: http://openradar.appspot.com/radar?id=5032957332946944

Current lousy workaround:

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

Swift 3.0:

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

Refer the below coding

@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.")
}

You need to make sure you have the correct frame for your imageview and the contentmode for your imageview should be aspectfit and the image returned by the picker is of type [UIImagePickerControllerOriginalImage]

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

Swift:

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

and coming to the allowsEditing property, It is not related to photo selection.

It is a Boolean value indicating whether the user is allowed to edit a selected still image or movie.

If you want to select photos from camera library, then you need to modify the source type to

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

Update:

LEts say you have selected one image and display it on the image view. I assume you have one imageview on the view, and the imageview frame is equal to the view frame. If the IB is of freeform , I assume the size 600x600 for the imageview frame in the IB.

if you simply do:

     _startImageView.image=_img;

The result will be:

Now let's make some changes to the image to be displayed in the 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;

and now the image will be:

The original image choosen is of size 640x426, when not scaled.

The original image choosen is of size 1536x1286 when scaled to max(with two finger zoom action).

As you can see, still there is no huge change in the image, it is because the image is already cropped/scaled by the time your imageview recieved the image!!!

So even though you try to do:

      [_img drawInRect:_startImageView.frame];

The image will not be drawn as per our need, as the image is already scaled right at them moment as the image given by the picker is the edited image.

Solution:

To fix this, you need to select the original image from picker in didFinishPickingMediaWithInfo: method

       info[UIImagePickerControllerOriginalImage];

which gives you the image:

This code is relatively similar to @pkc456, just a little shorter.

This works perfectly for me:

   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)

}

Does this work?

I am working on xcode 7.1(Swift) and found your code appropriate. I also wrote the below code on my project and it is working successfully.

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

The only difference in your code and my code is about visibility of ImagePickerController instance. For your reference, I upload my code at:- https://www.dropbox.com/s/pe0yikxsab8u848/ImagePicker-Swift.zip?dl=0

My idea is just look at my code once, may be you will get an idea about which section of your code malfunctioning.

It seems like you are using IB and auto layout but have set a right constraint. Try to add some constraint in your storyboard.

for me I was solved, showing mode as 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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!