Question

Hello I am trying to resize a UIImage, but even though I'm not getting any errors it is not working.

hers the code of .h file

IBOutlet UIImageView *Fish;

heres the code of .m file

Fish.frame = CGRectMake(0, 0, 300, 293);

What am I doing wrong? Thanks for any help

Was it helpful?

Solution

The image is probably not resizing because you are just resizing the image view. Make sure in your storyboard that you make the image view (Fish), have the move ScaleToFill. I can't do screenshot due to reputation ( sorry :( )

Alternately, if your goal is not to resize the image view but to resize the image it is holding, you can do this:

UIImage *image = Fish.image;
UIImage *image = YourImageView.image;
    UIImage *tempImage = nil;
    CGSize targetSize = CGSizeMake(80,60);
    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
    thumbnailRect.origin = CGPointMake(0.0,0.0);
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;

    [image drawInRect:thumbnailRect];

    tempImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    YourImageView.image = tempImage;

and you would set thumbnailRect to whatever size you want.

Hope this helps! Please search Nerdy Lime on the app store to find all of my apps! Thanks!

OTHER TIPS

I bet your outlet is not hooked up. In your "viewDidLoad" method, try doing this:

if(Fish)
{
    Fish.frame = CGRectMake(0, 0, 300, 293);
} else {
    NSLog(@"Fish is null; why did I forget to connect the outlet in my storyboard or xib?");
}

And this isn't the best way to resize your UIImageView. If you're using regular springs & struts, you can grow an outlet by clicking the springs & struts to grow based on the superview's size, e.g.:

Grow Everything

And if you're doing AutoLayout, there's a different thing you can do (basically pin your view to all four sides of the superview).

Here is how I do it:

1) select the outlet / object you want to add constraints to (in your case, it'll be the fish image view)

2) see the segmented control at the bottom of the Interface Builder window? Click on the second one and you'll see a popover view open up with a list of possible constraints to add.

3) In my example, I'm adding constraints in my ImageView to always be 10 pixels from each edge of the superview (note the four "10"s and solid red lines meaning I'm adding four constraints).

AutoLayout is a pain to get accustomed to (and I'm still learning it myself), but I suspect that once one gets the hang of it, it'll be a powerful new tool especially as Apple brings in additional iOS screen sizes in the very near future.

Add New Constraints

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top