how to crop some portion of scaled, rotated image-view - iphone

I am unable to crop he image from image view, which is scaled and rotated before cropping. used existing cropping methods.but its applicable only for image-view which is fixed.Hope code will helpful.

Finally succeeded cropping rotated scaled image.
1) Need to crop the entire scaled or rotated image.
2)crop the rect from cropped Image. you will get final scaled or rotated cropped image.
Im posting sample code , it may helpful for others.
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = CGRectMake(100,100 ,200, 200);//Rect we need from scaled or rotated image.
CGImageRef cropped = CGImageCreateWithImageInRect(viewImage.CGImage, rect);
UIImage *img = [UIImage imageWithCGImage:cropped];
CGImageRelease(cropped);
Thanks.

Try This(if you need to crop the corners):
UIImageView *imageSubview = [[UIImageView alloc] initWithFrame:CGRectMake(50.0f, 5.0f,80.0f, 60.0f)];
[imageSubview setImage:actualImage];
[imageSubview setContentMode:UIViewContentModeScaleToFill];
[imageSubview.layer setCornerRadius:10.0];
[imageSubview.layer setMasksToBounds:YES];
[cell addSubview:imageSubview];
Just increase the radius and you will make it circle, or walk on this path you'll get your actual solution.:)

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);
its for cropping particular height and width

Related

how to crop image on camera in iphone?

Hi am using Camera Application. I need to crop image in particular area of image, which has been taken and used by UIImagepicker. I done with adding overlay in camera and croping image. but i got cropped image is not for my Rect size.
Try using the UIImage+ProportionalFill category by Matt Gemmell. This category extends UIImage by cropping/scaling capabilities.
Croping start from x=0; y=0; and till width=125 and height=128;
UIImage *image =imageView.image;
CGRect cropRect = CGRectMake(0, 0, 125,128);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
[imageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);

How to crop Image using rectangle?

I am trying to crop an image using this rectangle.But Not getting how to stretch the rectangle.I am having one UIView that is outside and within that other view shown with black border. When I stretch DO i need to change the frame for both UIViews ??
should use Pinchgesture to stretch the rectangle or any other method??
Here four blue handlers are UIView s. How will i determine the dragging of them??
If there please suggest.
Following code for the selecting the box and cropping the image.
-(IBAction) cropImage:(id) sender{
// Create rectangle that represents a cropped image
// from the middle of the existing image
float xCo,yCo;
float width=bottomCornerPoint.x-topCornerPoint.x;
float height=bottomCornerPoint.y-topCornerPoint.y;
if(width<0)
width=-width;
if(height<0)
height=-height;
if(topCornerPoint.x <bottomCornerPoint.x)
{
xCo=topCornerPoint.x;
}
else
{
xCo=bottomCornerPoint.x;
}
if(topCornerPoint.y <bottomCornerPoint.y)
{
yCo=topCornerPoint.y;
}
else {
yCo=bottomCornerPoint.y;
}
CGRect rect = CGRectMake(xCo,yCo,width,height);
// Create bitmap image from original image data,
// using rectangle to specify desired crop area
UIImage *image = [UIImage imageNamed:#"abc.png"];
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(110, 600, width, height)];
imageView.image=img;
[[self view] addSubview:imageView];
[imageView release];
}
Hope this may helping to lot

How can I resize my UIImage?

Here is the code I am using to resize my UIImage, but unfortunately it isn't working:
UIImage *cellImage = [UIImage imageNamed:#"warning.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0);
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], cropRect);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
Why does this code not work?
Thanks
HI try this:-
UIGraphicsBeginImageContext(CGSizeMake(50,50));
[[UIImage imageNamed:#"warning.png"] drawInRect: CGRectMake(0, 0,50,50)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Are you forgetting to add myImageView as a subview of your main view or another visible view? E.g.
[cell.contentView addSubview:myImageView];
This would explain why nothing appears.
However, CGImageCreateWithImageInRect() crops the image within the rect if your rect is smaller than the image. To resize the UIImage itself, see: The simplest way to resize an UIImage?
However, it's easiest if you just let the UIImageView handle the resizing if you don't actually need the UIImage itself resized.
UIImage *cellImage = [UIImage imageNamed:#"warning.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0);
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], CGRectMake(0.0f,0.0f,cellImage.size.width,cellImage.size.height));
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
[cell.contentView addSubview:myImageView];
See:
UITableViewCell Class Reference: contentView
I don't know why that doesn't work but try using UIGraphicsBeginImageContext and UIGraphicsEndImageContext instead, UIGraphicsBeginImageContext takes a size.

Rotate UIImage in drawRect

I have the following code that displays an image when the drawRect is called.
UIImage *sourceImage = [UIImage imageNamed:#"icon.png"];
CGRect rect = CGRectMake(12.5, 12.5,
sourceImage.size.width,
sourceImage.size.height);
[sourceImage drawInRect:rect];
How can I get this rotated by a number of degrees before it gets drawn, everything I read needs it in a ImageView which seems heavy in a drawRect
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIImageView *imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
imageView.image = image;
[self addSubview:imageView];
CGAffineTransform rotate = CGAffineTransformMakeRotation( 1.0 / 180.0 * 3.14 );
[imageView setTransform:rotate];
Rotating (or any other transformation done by a transformation matrix) is not "heavy". All the transforms basically are simple multiplications of a vector and a small matrix (see Apple's Quartz 2D documentation for the math)
Use CGContextRotateCTM(context, radians); before you draw the image with [sourceImage drawInRect:rect];. If you need to rotate the image around another point, translate with CGContextTranslateCTM first.

Setting a UIImage border

Is there a way to set borders to a UIImage. I know how to set it for UIImageView, but my problem is the UIImage that I load up in a UIImageview wont be of the same size or aspect ratio as the UIImageView. Hence Ive kept the UIImageView mode to aspect fit. Giving a border to UIImageView now would border the entire UIImageView rather than just the UIImage, and that doesn't look good when the UIImage is not of the same size or aspect ratio as the UIV.
Help?
There's some solutions in this question: How can i take an UIImage and give it a black border?
#import <QuartzCore/CALayer.h>
UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;
imageView.layer.cornerRadius = 10; //optional
image with border
+(UIImage*)imageWithBorderFromImage:(UIImage*)source
{
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}