15

I've read various posts on here asking similar questions... I've tried various ways that were posted including bounds and frames etc. including the following:

myImage.frame = CGRectMake(0.0f, 0.0f,50.0f, 50.0f);

and:

myImage.bounds = CGRectMake(0.0f, 0.0f,50.0f, 120.0f);

neither of those work.

However, I find it interesting that the following code let's me move the Image around but doesn't change the width:

CGRect frameRect = myImage.frame;
frameRect.size.width = 50.0f;
frameRect.origin.x += 10.5f;
myImage.frame = frameRect;

So why don't any of these change the width/height of my ImageView?

I found another post on here that basically states I have to right a small book of code to get it resize my image... is that true?

Such as this one: UIImage: Resize, then Crop

certainly this is simpler than that??

Community
  • 1
  • 1
  • I figured it out, my question wasn't asked the best way, I should have stated that it was ok to scale the image. I was able to just select to the mode to be "scaled to fill" and the code I have above works just fine then. Thanks for the help. –  Jul 14 '09 at 02:41

8 Answers8

27

The following will change the size of the UIImaveView, clipping the underlying image without resizing it and keeping it aligned to bottom left of view:

imageView.frame = CGRectMake(
             imageView.frame.origin.x, 
             imageView.frame.origin.y, newWidth, newHeight);

imageView.contentMode = UIViewContentModeBottomLeft; // This determines position of image
imageView.clipsToBounds = YES;
Vladimir
  • 170,431
  • 36
  • 387
  • 313
Matt Mendrala
  • 279
  • 3
  • 2
11

First off, you can't set the frame or bounds of the UIImage - that will only work on a UIImageView.

I've found that changing the frame of a UIImageView causes the Image to be scaled to the new size. Sometimes, that's undesirable - and you want instead to crop the image.

I can't tell if this is what you're asking for, but here's some code to crop an image to a specific size in a UIImageView:

UIImage *myImage = [UIImage imageNamed:@"photo.png"];

CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);

UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 

CGImageRelease(croppedImage);
Glenn Barnett
  • 2,031
  • 1
  • 21
  • 31
3

From what I get of the question, the OP wanted to change the size of the UIImageView when the size of the container UIView is changed. The code below will do it...

UIView * foo = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)] autorelease];
foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIImageView * bar = [[UIImageView alloc] initWithImage:
                     [UIImage imageNamed:@"test.png"]];
bar.autoresizingMask = foo.autoresizingMask;
[foo addSubview:bar];
[self.view addSubview:foo];

The key here are the foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight and the bar.autoresizingMask = foo.autoresizingMask; lines. Forget either of these, and the whole jigmarole will stop working.

Manav
  • 10,094
  • 6
  • 44
  • 51
3

Well, if your read the documentation about UIIMage you can notice that is impossible to change any parameter of an UIImage after create it, the solution I've implemented for use high quality images for change some parameter of (for example) the SliderControl as Screw Image, is the next one:

UIImage *tumbImage= [UIImage imageNamed:@"screw.png"];
UIImage *screw = [UIImage imageWithData:UIImagePNGRepresentation(tumbImage) scale:2];

With that, I can to use 100x100 px image in my apps scaled to 50%.

Kind regards.

pabloverd
  • 614
  • 8
  • 8
1

Try Using a UIScrollView. Add the UIImageView to the UIScrollView in Interface Builder you can then control the position and size of the image as follows:

    CGRect rect = [scrollView frame];

    rect.origin.x = 50.0f;
    rect.origin.y = 0.0f;
    rect.size.width = 320.0f;
    rect.size.height = 150.0f;

    [scrollView setFrame:rect];
1

If you tried those methods cannot work, the only way to do it is to add the constraint of width and height to the UIImageView.

// Create the constraint in code
NSLayoutConstraint *constraint0 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant: yourNewsWidth];


NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant: yourNewsHeight];

[myImage addConstraint:constraint0];
[myImage addConstraint:constraint1];
Weidian Huang
  • 2,787
  • 2
  • 20
  • 29
0

Use myImageView.frame = myNewPosAndSize; to resize or reposition your image view (as with any other view). It might confuse you that the image view draws its image with its original size, possibly exceeding its own dimensions. To disable this use myImageView.clipsToBounds = NO;

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
-1

You don't have to write a whole book, you can copy that code.

I believe the UIImageView always draws the image at 0,0 at a 1.0 scale. You'll need to resize the image if you want to continue using the UIImageView.

Ecton
  • 10,702
  • 2
  • 35
  • 44