4
votes

I am having trouble with UIView which is feeding by an image from Camera Roll.

When I pick a picture which was taken in the portrait mode the view always stretch the picture too high, but when I rotate my device the picture is stretched fine.


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if(info){
        {
            [artView removeFromSuperview];
            artView = nil;
            artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

            artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
            artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            artView.contentMode = UIViewContentModeScaleAspectFill;

[self.contentView addSubview:artView];

Any help is appreciated.

2
I have more or less the same problem. When I take a picture, this works just fine. When I pick a photo from the camera roll, no dice. In my case I'm using UIViewContentModeScaleAspectFill, setting clipsToBounds to YES, and restricting the frame to the device width and 100.0 height. It ends up fitting the entire image into the height vs. filling the frame with 100pts worth of image. Try this with a camera shot, however, and it does the correct thing. - Joe D'Andrea

2 Answers

5
votes

If anyone else is still having this problem try setting the image after contentMode.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if (info) {
        [artView removeFromSuperview];
        artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

        artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        artView.contentMode = UIViewContentModeScaleAspectFill;
        artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

        [self.contentView addSubview:artView];
        [artView release];
    }
}
2
votes

Your code looks good except it leaks and you have too many opening braces and no closing braces.

You probably want it to look like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if (info) {
        [artView removeFromSuperview];
        artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

        artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        artView.contentMode = UIViewContentModeScaleAspectFill;

        [self.contentView addSubview:artView];
        [artView release];
    }
}

UIViewContentModeScaleAspectFill shouldn't stretch the image. It should clip it. Perhaps UIViewContentModeScaleAspectFit is what you're looking for?