6
votes

I have a large image (1920*1080), and a smaller UIImageView (320*568) that scaled to full screen size in the story board.

Now I want to display this large image full screen size, but fit to the UIImageView.

I have tried all the content mode, but they are all not working. Every time it just shows the top left part of the image full screen sized.

 [self.imageView setFrame:self.view.bounds];
 [self.imageView setContentMode:UIViewContentModeScaleAspectFit] ;
 [self.imageView setImage:image] ;
 [self.view insertSubview:_imageView aboveSubview:_previewView] ;

So what might be wrong in my case? could that be a Xcode story board configuration error ?

thanks.

4
can you please upload screenshot if it possible? so we can get idea. what you are facing.. - Jatin Patel - JP
This happens to me sometimes too, when I make an image using CIFilters so I get a CIImage and convert to UIImage some device architectures create a corrupt image which won't respect contentModes. It will work on device but fail on simulator for example. It's an Apple bug but you can solve it by manually sizing the UIImageView to what you need, snapshotting it w/ CGContext, then using that CGImage instead and putting UIImageView back to the frame/bounds you needed. Annoying work-around, but Apple hasn't fixed this bug for years. - Albert Renshaw

4 Answers

5
votes

If you are seeing only the image top corner in your ImageView, then the possible reasons are: Your ImageView frame is out of screen size OR Content Mode is not set. In your code you are setting the frame as the bounds of the superview. If your are using Auto-layout, check the auto-layout are set properly. If this is ok, then try setting UIImageView ContentMode before setting the frame.

// Setting the content mode.
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
// Now set the frame.
[self.imageView setFrame:self.view.bounds];

Other solution is you can downscale your image to fit your ImageView. For that you can use the following function:

- (UIImage *)scaleImage:(UIImage *)orginalImage
{
    float widthFactor = photoImageView.frame.size.width / orginalImage.size.width;
    CGSize destinationSize = CGSizeMake(orginalImage.size.width * widthFactor,orginalImage.size.height * widthFactor);
    UIGraphicsBeginImageContext(destinationSize);
    [orginalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    photoImageView.image = scaledImage;
    return scaledImage;
}
3
votes

There is a problem that CIImage does somehow not respect contentMode (at least on some devices / iOS versions). Convert CIImage to a CGImage to make this work.

Swift 3 code:

func convertCIImageToCGImage(inputImage: CIImage) -> CGImage?
{
    let context = CIContext(options: nil)
    if let cgImage = context.createCGImage(inputImage, from: inputImage.extent)
    {
        return cgImage
    }
    return nil
}
2
votes

Try this

[self.imageView setFrame:self.view.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit; 
self.imageView.clipsToBounds = YES;
[self.imageView setImage:image] ;
[self.view insertSubview:_imageView aboveSubview:_previewView] ;

Working fine .. :)

2
votes

In my case, i solved this removing some constraints that resize the imageview that i set by error.

Check your imageview constraints, try removing all of them