How to set photo taken with UIImagePickerController to be viewed/modified into a custom UIViewController?

天涯浪子 提交于 2019-12-10 03:14:55

问题


I am currently using the default UIImagePickerController, and immediately after a picture is taken, the following default screen is shown:

My question is, how would I be able to use my own custom UIViewController to view the resultant image (and therefore bypass this confirmation screen ).
Please note that am not interested in using a custom overlay for the UIImagePicker with custom camera controls or images from photo gallery, but rather just to skip this screen and assume that the photo taken is what the user would have liked.

Thanks!


回答1:


Try this code to maintain your resultant image in same size:

First create IBOutlet for UIImageview.

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:nil];


    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        OriginalImage=info[UIImagePickerControllerOriginalImage];
        image = info[UIImagePickerControllerOriginalImage];
//----------
            imageview.image = image; // additional method
            [self resizeImage];


//---------
        if (_newMedia)
            UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil);

    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }

}



//---- Resize the original image ----------------------
-(void)resizeImage
{

        UIImage *resizeImage = imageview.image;

        float width = 320;
        float height = 320;

        CGSize newSize = CGSizeMake(320,320);
        UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0);
        CGRect rect = CGRectMake(0, 0, width, height);

        float widthRatio = resizeImage.size.width / width;
        float heightRatio = resizeImage.size.height / height;
        float divisor = widthRatio > heightRatio ? widthRatio : heightRatio;

        width = resizeImage.size.width / divisor;
        height = resizeImage.size.height / divisor;

        rect.size.width  = width;
        rect.size.height = height;

        //indent in case of width or height difference
        float offset = (width - height) / 2;
        if (offset > 0) {
            rect.origin.y = offset;
        }
        else {
            rect.origin.x = -offset;
        }

        [resizeImage drawInRect: rect];

        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

    imageview.image = smallImage;
    imageview.contentMode = UIViewContentModeScaleAspectFit;
}



回答2:


You can customize the image picker view controller using an overlay view.

Read the documentation here.

You set up a new view, set it as the camera overlay, tell the image picker to not display its own controls and display the picker.

Inside your the code of your overlay, you call takePicture on the image picker view controller to finally take the image when you are ready.

Apple has an example of this here: https://developer.apple.com/library/ios/samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196-Intro-DontLinkElementID_2

For transforms, you can use the cameraViewTransform property to set transformations to be used when taking the image.




回答3:


After taking the picture, the delegate method would be called:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

In this method, you dismiss the imagePickerController. You have access to the image, so you can play around with the image, possibly display it in a UIImageView. So the code would be something like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        //here you have access to the image 
        UIImage *pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        //Create an instance of a view controller and show the UIImageView or do your stuff there. According to you business logic.
    }
}


来源:https://stackoverflow.com/questions/20713357/how-to-set-photo-taken-with-uiimagepickercontroller-to-be-viewed-modified-into-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!