简体   繁体   中英

UIImagePickerController imagePicker not fitting to iPhone 4 Screen

I get the user to pick an image with UIImagePickerControllerSourceTypePhotoLibrary.

I then have the following method which gets the edited image, applies an overlay and then passes it to my PostPictureViewController for display. On display I want the image to maintain its original Aspect Ratio and fit it to the screen.

It works fine on the simulator using 4.3 sdk, but when I put it on an iPhone 4 retina display screen, the image is zoomed to the top left corner and only 1/4 of image is displayed full screen getting rid of the original aspect ratio!

How can i shrink this down to fit the screen like it works on the simulator?

I have also tried getting rid of the overlay completely and just returning the original and edited image to display, but even that does not keep its original aspect ratio!!!

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{        
    // Access the cropped image from info dictionary
    image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    // Combine image with overlay before saving!!
    image = [self addOverlayToImage:image];

    overlayGraphicView.image = nil;

    // Take the picture image to the post picture view controller
    postPictureView = [[PostPictureViewController alloc] init:image];    
    [picker pushViewController:postPictureView animated:YES];

    [picker release],picker = nil;
}

Here is my Adding Overlay Image function:

- (UIImage*) addOverlayToImage:(UIImage*)originalImage
{
    //CGRect cgRect =[[UIScreen mainScreen] bounds];
    CGSize size = originalImage.size;

    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        // Use for iPhone 4 Retina
        UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
    } else {
        UIGraphicsBeginImageContext(size);
    }

    UIImage* overlayImage = [UIImage imageNamed:overlayGraphicName];

    [originalImage drawAtPoint:CGPointMake(0,0)];

    [(UIImage *)overlayImage drawAtPoint:CGPointMake(0,(originalImage.size.height/2 - overlayImage.size.height/2) + 20)];

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    [finalImage retain];

    UIGraphicsEndImageContext();

    return finalImage;
}

and finally in my PostPictureViewController i display the image in the viewdidload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor blackColor]];

    // Load the picker image for viewing
    UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 480)];
    [pickerView setImage:pickerImage];
    pickerView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:pickerView];
    [pickerView release],pickerView=nil;
}

In this viewdidload method for displaying the image, if i do this:

UIImageView *pickerView = [[UIImageView alloc] initWithImage:pickerImage];

then the image is not centered to the screen vertically on the simulator, and starts at 0,0, where as actually defining the frame width and height with CGRectMake centers it on the screen.

Obviously, for iPhone 4 I have tried changing this too:

UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640, 960)];

But the image is still scaled up to high and bigger then the screen!

您可以在界面构建器中将imageview的属性设置为Aspectfit。这会有所帮助。或者通过编程,您可以使用imageView.contentMode = UIViewContentModeScaleAspectFit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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