UXDE dot Net

Image Cropping Application Source Code for iOS

By -

BJ Image Cropper :

BJ Image Cropper is Simple UIVIEW  that allows user to do cropping using a cropper which is displayed over an image and you can drag cropper over image areas and do cropping.For this you will need already build .h and .m file which i will provide including whole application . you can get that application by reading above note.

How this application Works ? :

  • When this application start running you need to select a picture from camera roll and then image is loaded into the UIIMAGEVIEW .
  • After this You need to press the button Crop when u Touch a cropper will appear on a subview of image and then you have to select your cropping area by touching the ends of rectangle appearing on that Subview.
  • After this You have to touch another button which takes crop image into a Dummy Variable of type UIIMAGE and then you have to  put that Dummy variable into the UIIMAGEVIEW .

The Reason for explaining this nontechnical part is to let you people understand the flow of the program Now Again.

Touch Select button              = Image loaded into UIIMAGEVIEW

Touch Crop button                = Image from UIIMAGEVIEW is loaded into  UIVIEW and a Cropper is appeared on that UIVIEW which is able to drag.

Touch Apply Crop Button  = Cropped Image from the UIVIEW IS Loaded into UIIMAGE *Image and then into UIIMAGEVIEW = image ;

Now here Comes the Code

  •  This is update is Used to update the gui mask or rectangle which is on UIVIEW.
  • Size of this Rectangle can also be changed by holding clicks at the ends of rectangle corners.
  • In this function if origin is changed then a new origin with same height and width is update
  • If height and width is changed then a new height and width is update.
  • Both above task can be done simultaneously.
  • This function uses BJIMAGECropper.h and BjimageCropper.m which will be provided if you need just comment.
UIVIEW

UIVIEW

 

- (void)updateDisplay {
self.boundsText.text = [NSString stringWithFormat:@"(%f, %f) (%f, %f)", CGOriginX(self.imageCropper.crop), CGOriginY(self.imageCropper.crop), CGWidth(self.imageCropper.crop), CGHeight(self.imageCropper.crop)];

if (SHOW_PREVIEW) {
self.preview.image = [self.imageCropper getCroppedImage];
self.preview.frame = CGRectMake(10,10,self.imageCropper.crop.size.width * 0.1, self.imageCropper.crop.size.height * 0.1);
}
}

 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isEqual:self.imageCropper] && [keyPath isEqualToString:@"crop"]) {
[self updateDisplay];
}
}

-(void) viewWillAppear:(BOOL)animated{
// No need to store… this is 1 use only anyway. Save memory, and release it when done.
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;

}

  •  This function crops the image this function takes coordinates of mask or rectangle from gui and subtract all pixels from that location to end corners.
Crop

Crop

 

-(IBAction)getcorp:(id)sender{

initWithFrame:CGRectMake(10,10,self.imageCropper.crop.size.width * 0.1, self.imageCropper.crop.size.height * 0.1)];
UIImage *im = [self.imageCropper getCroppedImage];
self.image_view.image = im;
for(UIView *sub in [self.view subviews])
{
if([sub isKindOfClass:[imageCropper class]])
[sub removeFromSuperview];
}
}

  •  This function creates a subview and extract image from UIImageView and creates a mask or rect on that image which is displayed on UIVIEW.
  • Means it creates a cropper on that gui UIVIEW where you image is displayed.
Subview

Subview

-(IBAction)corp:(id)sender{

CGFloat height = image_view.bounds.size.height;
CGFloat width = image_view.bounds.size.width;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tactile_noise.png"]];

self.imageCropper = [[BJImageCropper alloc] initWithImage:img andMaxSize:CGSizeMake(height,width) ];
[self.view addSubview:self.imageCropper];
self.imageCropper.center = self.view.center;
self.imageCropper.imageView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.imageCropper.imageView.layer.shadowRadius = 3.0f;
self.imageCropper.imageView.layer.shadowOpacity = 0.8f;
self.imageCropper.imageView.layer.shadowOffset = CGSizeMake(1, 1);

[self.imageCropper addObserver:self forKeyPath:@"crop" options:NSKeyValueObservingOptionNew context:nil];

if (SHOW_PREVIEW) {
self.preview = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,self.imageCropper.crop.size.width * 0.1, self.imageCropper.crop.size.height * 0.1)];
self.preview.image = [self.imageCropper getCroppedImage];
self.preview.clipsToBounds = YES;
self.preview.layer.borderColor = [[UIColor whiteColor] CGColor];
self.preview.layer.borderWidth = 2.0;
[self.view addSubview:self.preview];
}

}

  •  This function accesses camera roll.
CameraRoll

CameraRoll

-(IBAction)next:(id)sender{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:self.imgPicker animated:YES];
}

 

  •  When Image is picked picker should be dismissed.

-(void)imagePickerControllerDidCancel :( UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}

 

  •  Picks image from from camera roll.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

self.image_view.image=img;

}

This the variable mask which is created on that gui UIView.

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef maskRef = maskImage.CGImage;

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}

- (void)viewDidUnload
{
[super viewDidUnload];
[self setImageCropper:nil]; //image croper
[self setBoundsText:nil]; // image croper
[self.imgPicker release]; // Release this here, this will execute when modal view is popped.

}

 

 

- (void)viewDidUnload
{
[super viewDidUnload];
[self setImageCropper:nil]; //image croper
[self setBoundsText:nil]; // image croper
[self.imgPicker release]; // Release this here, this will execute when modal view is popped.
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

For More Source codes  visit Image filter Processing Implementation in Xcode for iPhone Using GLImageProcessing iOS Developer Library

Farheen Bibi

You can find Farheen on , and .

74 Comments to Image Cropping Application Source Code for iOS

  1. You have to wait for this project first you have to explain about your problems and your knowing about this and also you will get whole source code first share your experience you will automatically receive xcode source code image cropping in your mail after few days when our PROGRAMER Allow us to share this according to your skills
    thanks regards@crunchmodo please read last email carefully and do what was asked to do

    • Hi could you please share your source code i am create an ios app and for which i have a screen where user is allowed to upload the image from photo library, so what i need is to allow them to crop that image. I have many forums and found that there is not readymade API in ios, like android to accomplish this job. I have downloaded somethin from GIT but having few bugs in it. So if your code is perfectly working please send me.

    • There are two kinds of application
      1)Picks image from camera Roll and a cropper appears on Image View and desired area is selected to crop image.
      2)The other application directly uploads the image into the UIView and image is provided into the xcode resources

      Which one do you want ?

  2. I am building an app to mat artwork and need to crop and scale and rotate image before building a mat around image. Trying to to use same concept of drawing rects around stage but must be easier way saw this post.

    • This application is only Cropping input image.I also have Developed an application which scales the image.Keep on visiting us soon i will make a post which will provide you an application according to your given requirements.Also Let us know in which language you want to implement all these features.Thanks @Crunchmodo Team

    • Sorry dude right now code isn’t available you have to wait for 2-3 days for this code i really apologize for this but i will provide you soon mac system on which i have made this application is in my office and right now i am off for 2-3 days. thanks please keeep visiting to get that code thanks

  3. Computer engineer from St. Louis, MO, and enthusiast iOS newbie. I am studying image manipulation, Quartz, and other related frameworks just for fun.

    Would love to see the Xcode project in action.

  4. Hi, thank you very much for sharing this. May you send me the code? It would be very helpful for my computer vision project.

  5. Hi, thank you very much for sharing this. May you send me the source code? It will be very helpful for my computer vision project.

      • I am working on a shape recognition project. A part of the application is that i need to grab the picture from the photo library, crop an area and then apply some image processing to the cropped part.

        • Thats very amazing can you share with us how much you have completed your project i just want to know techniques which you will be using and i am also working on shape recognition actually its text recognition from an image

  6. Gabriel Lupu

    Hey, can I get the code please for cropping Image on IOS? I need this for a small project where i have an Image and crop it. Thank you

  7. Sumir Bharati

    Hi,

    Im developing the healthcare solution for nurses, so that they can take a picture of wound and forward to doctors for treatment targeting villages in india.

    i need this source code, can you please provide me the code.

  8. Hi Ammad,

    I had used the orignal BJCropper for iphone, but if the size of the image is greater than the frame of the view, it doesn’t work.Can you share your project with me.it will be helpful for me.

    Email: akhil.arora007@gmail.com
    Thanks

    • Yea i made changes i think i mailed you the other one and the project shown in the post is in my mac i will mail it to you tomarrow cuz i havn’t access to mac

  9. LIN Jianbin

    Hi, now I am working on a project that wants to allow user to crop image caught from the camera using UIImagePickerController. Can I use you code in this project? Please email to have a talk or just send me the code. Thanks.

  10. New to iOS, i am developping an app where the user can get the image from camera or camera roll (done), display this image into a new view (done), crop the image (this is where I need this!) and then I use the cropped image to do some stuffs.

    Could you please send me the source code? Regards,

  11. JuthathipK.

    Hi Mr.Ammad :)

    i am Tiffy , i study in Thailand and begin to do or learn to develop project in Xcode ( sorry,if i write a wrong sentence because i stupid in English >__> user take pictures or choose existing pictures and show image to UIScrollView (Horizontal)
    and i need to edit process >> crop image before show in UIImageView in Rectangle or drag a frame to choose a crop
    then i want to know your source code is help me?
    if your source code can help me, i appeal you send a source code to me in learning process of you

    Thanks to read my comment ^^
    Email: tiffey5021@gmail.com , i_missky90@hotmail.com

  12. Hi,

    I would need the code to try this. I am implementing something similar with variations. Can you please mail me the code?

    Thanks,
    Ajesh

  13. Sulekha Sharmila

    Respecte sir,
    this is me sulekha..Im doing a profect on face recognition..so i need a java code on to display and crop the image…so please help us sir…

  14. Sir nice job can I ask a code for this?
    I am currently learning on image implementation can you please send me a source code for this? thanks

  15. Hey there,

    I’d love the code so please share.

    Do you think it is possible to rotate the crop box too?

  16. Could you please sent me the source,too. Basically what I need is picking an image from camera roll, show a crop rectangle which can be changed only at the edges an preserves the aspect ratio. get the coordinates of the area which was selected and get the cropped image.

    Thanks in advance.

  17. Tai Nguyen

    may you send me the code, I’m building a small app for crop image that was taken from camera or chosen from galery, thanks

  18. Hi there,

    I’m building an image edit tool for IOS and I would love to see this source code. Could you please send me the files?

    Thank you!!!

  19. Hi, thank you very much for such anice tutorial. May you send me the source code? It will be very helpful for me.

  20. Hello Can you please suggest the link from where I can download the source code of this tutorial. It will be a great help for me..:)

  21. hi ammadkhan,
    Still i wont get any mail from u dude…!
    so pls try it again i need to completed that task in today itself dude….!

Leave a Reply