Clipping UIImageView if is moved out of CGRect - iphone

I want to clip an UIImageView if it moves out of given rect.
The image is moving across the screen by setting the frame, but I need to clip it when it moves out of a given fixed rect (say (0,0,650,650)).

If you only want to show the center of the image an a UIImageView set the content mode to "Aspect Fill" via IB. That will center it and crop off anything extra.
Or in code
myImageView.contentMode = UIViewContentModeScaleAspectFill;
or
Subclass UIView and in the drawRect method, adjust the rect and draw out the clipped image using CGContextDrawImage (context, rect, sourceImage.CGImage).
or
Use CALayers. You can adjust the 'contentsRect' of the imageView's layer:
CALayer* imageLayer = imageView.layer;
imageLayer.masksToBounds = YES;
CGRect rect;
if (inPortrait)
rect = CGRectMake(0.0, 0.0, 0.5, 1.0); // half size
else
rect = CGRectMake(0.0, 0.0, 1.0, 1.0); // full size
imageLayer.contentsRect = rect;

Did you try using the property clipsToBounds of UIView and setting it to YES as it says in Reference that
Setting this value to YES causes subviews to be clipped to the bounds of the receiver
UPDATE
See if this SO question helps you
Proper use of UIRectClip to scale a UIImage down to icon size

myImageView.contentMode = UIViewContentModeCenter;
Try this.

Related

How to show in UIImage just a part of image?

I have UIImageView in which I'm showing 50x100 image.
I want to show only a part of image 50x50 (top part)?
How can I do that?
The very simple way to move big image inside UIImageView as follows.
Let we have the image of size (100, 400) representing 4 states of some picture one below another. We want to show the 2nd picture having offsetY = 100 in square UIImageView of size (100, 100).
The solution is:
UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
CGRect contentFrame = CGRectMake(0, 0.25, 1, 0.25);
iView.layer.contentsRect = contentFrame;
iView.image = [UIImage imageNamed:#"NAME"];
Here contentFrame is normalized frame relative to real UIImage size.
So, "0" means that we start visible part of image from left border,
"0.25" means that we have vertical offset 100,
"1" means that we want to show full width of the image,
and finally, "0.25" means that we want to show only 1/4 part of image in height.
Thus, in local image coordinates we show the following frame
CGRect visibleAbsoluteFrame = CGRectMake(0*100, 0.25*400, 1*100, 0.25*400)
or CGRectMake(0, 100, 100, 100);
You can crop the image by using CGImageCreateWithImageInRect, which is Quartz primitive working on CGImageRef, so you would have something like:
CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropRect);
UIImage* outImage = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation]];
CGImageRelease(imageRef);
When calculation cropRect, keep in mind that it should be given in pixels, not in points, i.e.:
float scale = originalImage.scale;
CGRect cropRect = CGRectMake(0, 0,
originalImage.size.width * scale, originalImage.size.height * 0.5 * scale);
where the 0.5 factor accounts for the fact that you want the top half only.
If you do not want to go low-level, you could add your image to a UIView as a background color and use the clipToBounds CALayer property to make the clipping:
myView.backgroundColor = [UIColor colorWithBackgroundPattern:myImage];
myView.layer.clipToBounds = YES;
also, set myView bounds accordingly.
I might have a solution for you. See if this works. In Interface Builder there is an option about Image content fill properties. You can set it to top-left. Follow the image in interface builder -
After this set the size of UIImageView to 50x50 with "clip-subviews" checked...

Clipping an image with a custom UIBezierPath

I was wondering if someone could point me in the right direction with this problem.
I have a user-created UIBezierPath with several points that have come about by user touches.
I can get these to create shapes on a bog-standard UIView by using the [myPath fill]; function.
What I would ideally like to do is to use the path to create a clipping mask for a UIImage - perhaps as a UiImageView?
I tried changing my UIView to a UIImageView and then changing the function to [myPath addClip]; but for some reason it does not work.
You need to create a graphics context, draw your image, and draw your mask using kCGBlendModeDestinationIn. Something like this:
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); // size is a CGSize of the result you want
[image drawInRect:imageRect]; // image is the UIImage you want to clip, imageRect is the rect in the context where you want your image
[mask drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0]; // mask is a UIImage containing the mask you drew, rect is the rect in the context where you want your mask - could match imageRect above
UIImage* uiImage = UIGraphicsGetImageFromCurrentImageContext();

How to resize a UIImageView to fit the underlying image without moving it?

I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.
How can I resize the frame so it's the same size as the underlying image, while ensuring that the center point of the UIImageView does not change?
If you change the bounds of a UIView the center will not change.
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = newImage.size;
imageView.bounds = bounds;
imageView.image = newImage;
try something along the following lines.. not tested
CGSize imageSize = image.Size;
CGPoint oldCenter = imageView.center;
// now do some calculations to calculate the new frame size
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imageView.frame = rect;
imageView.center = oldCenter;

Need help about contentMode and autoresizingMask of UIImageView

i have picture like this image it will present on Landscape Mode
so if rotate device to Portrait, how to present image like this
Use an UIImageView with these settings:
imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
imageView.contentMode = UIViewContentModeScaleAspectFit;
It looks like what you're trying to do is clip the image on the right, while leaving the top, left, and bottom the same.
Two things to try:
1) Subclass UIView and in the drawRect method, adjust the rect and draw out the clipped image using CGContextDrawImage (context, rect, sourceImage.CGImage).
2) Use CALayers. You can adjust the 'contentsRect' of the imageView's layer:
CALayer* imageLayer = imageView.layer;
imageLayer.masksToBounds = YES;
CGRect rect;
if (inPortrait)
rect = CGRectMake(0.0, 0.0, 0.5, 1.0); // half size
else
rect = CGRectMake(0.0, 0.0, 1.0, 1.0); // full size
imageLayer.contentsRect = rect;
This slices the image in half along width. masksToBounds takes care of clipping sublayers (in case you have them). You can tweak the contentsRect unit rectangle to adjust where you want the cutoff. You may also want to adjust the imageView's own bounds as well to match the size.
This has an added bonus in that adjusting the contentsRect is automatically animated so when you do the rotation the width animates in and out nicely.
// Set the layer contents
view.layer.contents = [image CGImage];
// Stretch so that contents overflows the edges and keeps aspect ratio
view.layer.contentsGravity = kCAGravityResizeAspectFill;
// Clip off the overflow
view.layer.masksToBounds = YES;
// Set the frame
CGRect frame;
frame.origin = CGPointZero;
frame.size = view.superview.bounds.size;
view.frame = frame;
// Make sure it resizes when the superview does
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
You will have to work with view's layer.
You can find the appropriate guidelines at https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html
Hope this helps.
Thanks,
Jim.

Drawing half image

Hi all i am having .png image.I want to draw only half of this image.If i call method drawInRect: on image with width being half the size of image,it will still draw the full image
in given rect.So how to draw half image
Before calling -drawInRect, try setting a clip mask on the current context:
UIImage *image = // your image, let's say 100x100
CGRect drawingRect = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f); // adjust the origin; size has to be the image's size
CGRect clippingRect = drawingRect; // copy drawingRect...
clippingRect.size.width = 50.0f; // ...and reduce the width
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context); // save current graphics state
CGContextClipToRect(context, clippingRect);
[image drawInRect:drawingRect];
CGContextRestoreGState(context); // restore previous state (without clip mask)
If it's in a UIImageView, you can change the frame to only fit half the image, then set [imageView setClipsToBounds:YES].
You could crop your existing image by using CGImageCreateWithImageInRect
Clip Rect did the magic. Thanks #thomas
CGContextClipToRect(context, clippingRect);