开发者

Programmatically prevent UIImageView from autoresizing

开发者 https://www.devze.com 2023-04-13 07:16 出处:网络
I would like to truncate one end of a UIImageView by changing the size of the frame dynamically. Of course, if it autoresizes it won\'t look \'truncated\', but will look shrunk. Theformer is the effec

I would like to truncate one end of a UIImageView by changing the size of the frame dynamically. Of course, if it autoresizes it won't look 'truncated', but will look shrunk. The former is the effect I would like, the latter not.

So far I've set up the UIImageView in IB and then hooked it up programmatically using IBOutlet. I then placed this in ViewDidLoad, but still get the autoresizing:

[self.stringImageView setAutoresizingMask:UIViewAutoresizingNone];
self.stringImageView.frame = CGRectMake(9.0f, 508.0f, 500.0f, 26.0f);

Any ideas how I can clip my UIImageView instead of having it resize?

EDIT 1

It should also be noted that in IB I have deselected Clip Subviews and Autoresize Subvi开发者_如何学Goews.


Use : stringImageView.contentMode = UIViewContentModeCenter; so the image you add to the UIImageView always keeps the same size and the same position in your view.


You need to clip the UIImage object first according to imageview's frame and then set it as image for image view. See also Cropping an UIImage


@implementation UIImage (Resize)

// Returns a copy of this image that is cropped to the given bounds.
// The bounds will be adjusted using CGRectIntegral.
// This method ignores the image's imageOrientation setting.
- (UIImage *)croppedImage:(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}


In Interface Builder set Mode in the View Tab to "Top" or whatever it should be.


Another more efficient way to go if you only wish to make part of the image visible, or for example animate its appearance,

is to set the content mode of the UIImageView to UIViewContentModeCenter (or any other alignment) and include it in a UIScrollView.

You will only need to change the frame of the scroll view and your image will be visually cropped without you having to create the cropped image (which would be a very high price if it is for animation purpose).

0

精彩评论

暂无评论...
验证码 换一张
取 消