0

I like to crop a image in UIImageView. Can you provide me the full source code that will helpful for me.

j0k
  • 22,600
  • 28
  • 79
  • 90
NandaKumar
  • 173
  • 1
  • 2
  • 17

4 Answers4

2

It may help You

 UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300


 CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
  UIImage* part = [UIImage imageWithCGImage:cgImg];
  UIImageView* Croppedimage = [[UIImageView alloc] initWithImage:part];

and Below is the link more-usefull

how to crop image in to pieces programmatically

Good luck

Community
  • 1
  • 1
NIKHIL
  • 2,719
  • 1
  • 26
  • 50
  • 1
    Thanks for your suggestion at last i found the answer in last Saturday. it was same as above code i use CGImageCreateWithImageInRect() to crop a image but i used to store the uiimageview as uiimage and then crop the image in the same uiimageview – NandaKumar Jun 13 '11 at 07:39
2

I used to crop the image in same UIImageview

1)Store the image uiimageview

2)Take the same uiimageview and store it as Uiimage

3)crop the image and restore it

The following code will help

-(IBAction)setCropItem:(id)sender {

UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finishedPicForCrop = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([finishedPicForCrop CGImage], CGRectMake(0, 45, 320, 420));
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);
[firstImageView removeFromSuperview];
secondImageView = [[UIImageView alloc] initWithImage:img];
[secondImageView setFrame:CGRectMake(0, 0, 320, 460)];
scrollView.contentSize = secondImageView.bounds.size;
[scrollView addSubview:secondImageView];

}

Community
  • 1
  • 1
NandaKumar
  • 173
  • 1
  • 2
  • 17
1
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
StartPoint = [touch locationInView:touch.view];
if (Img_Screen!=nil) {
    [Img_Screen removeFromSuperview];
    Img_Screen=nil;

}

}

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
EndPoint = [touch locationInView:touch.view];
[self finishedTouchInside];

}

 -(void)finishedTouchInside{

CGFloat width=EndPoint.x-StartPoint.x;
CGFloat hieght=EndPoint.y-StartPoint.y;
CGRect myImageRect = CGRectMake(StartPoint.x, StartPoint.y, width, hieght);
Img_Screen= [[UIImageView alloc] initWithFrame:myImageRect];

CGImageRef imageRef = CGImageCreateWithImageInRect([imgView.image CGImage], myImageRect);
// or use the UIImage wherever you like
[Img_Screen setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
[self.view addSubview:Img_Screen];
imageViewNew.image=[UIImage imageWithCGImage:imageRef];

}

here u can crop in imageView and cropped image will be shown in ImageViewNew

Nookaraju
  • 1,668
  • 13
  • 24
1

If you need a UI control to crop an image try SSPhotoCropperViewController. It’s a custom view controller that provides a simple, configurable and easy-to-use UI for cropping and scaling photos in iPhone apps.

Here is the tutorial and the source code on GitHub.

Ahmet Ardal
  • 1,162
  • 1
  • 11
  • 12