Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView content mode is not working

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.

like image 383
tomriddle_1234 Avatar asked Jul 03 '15 04:07

tomriddle_1234


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

How do I add actions to UIImageView?

Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.

How do I install UIImageView?

First you create a UIImage from your image file, then create a UIImageView from that: let imageName = "yourImage. png" let image = UIImage(named: imageName) let imageView = UIImageView(image: image!) Save this answer.

What is use of UIImageView?

The class provides controls to set the duration and frequency when animating multiple images. It also allows you to stop and start the animation. All images associated with a UIImageView object should use the same scale.


1 Answers

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;
}
like image 193
Vishnu Kumar. S Avatar answered Oct 19 '22 23:10

Vishnu Kumar. S