简体   繁体   中英

How to truncate a UIImage in iOS

How can I truncate the left side of an image stored in a UIImage object. Basically in certain situations I just want to show part of an image.

How can I do this on with the iOS sdk?

PS I tried changing the frame size of the UIImage but that just scales the image and distorts it.

A very simple way is to load the image into a UIImageView , and then add the view to another view. You can then position the image view so that its .frame.origin.x property is negative, which will place it off to the left. The parent view needs to have setMasksToBounds:YES called on it, or else the image view will still be fully-visible.

There are many other ways to achieve this effect as well, but this may be the simplest for you to implement.

to crop a UIImage, you can use one of the UIImage categories available out there, such as http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

For example, this frame will remove 100 pixel from the left side of a 200x200 pixel UIImage

 CGRect clippedRect = CGRectMake(100, 0, 100, 200);
 UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM