tags:

views:

293

answers:

2

Hi, I develop an application in which i process the image using its pixels but in that image processing it takes a lot of time. Therefore i want to crop UIImage (Only middle part of image i.e. removing/croping bordered part of image).I have the develop code are, - (NSInteger) processImage1: (UIImage*) image {

CGFloat width = image.size.width; CGFloat height = image.size.height; struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel)); if (pixels != nil) { // Create a new bitmap CGContextRef context = CGBitmapContextCreate( (void*) pixels, image.size.width, image.size.height, 8, image.size.width * 4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast ); if (context != NULL) { // Draw the image in the bitmap CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage); NSUInteger numberOfPixels = image.size.width * image.size.height;

NSMutableArray *numberOfPixelsArray = [[[NSMutableArray alloc] initWithCapacity:numberOfPixelsArray] autorelease]; }

How i take(croping outside bordered) the middle part of UIImage?????????

+2  A: 

try something like

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);

image = [UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);

cropRect is Smaller Rectangle with middle part of the image...

mihirpmehta
mihirpmehta,now i trying to do something like that,- (UIImage *) cropedImage: (UIImage *) image{ CGFloat width = image.size.width; CGFloat height = image.size.height; UIImage *cropedImage = [[UIImage alloc] init]; CGFloat widthCrop = (image.size.width)/2; CGFloat heightCrop = (image.size.height)/2; }Thereafter i can't visualize what to do?
Rajendra Bhole
get new X and New Y as OldX + OldX/4 and OldY + OldY/4 and make REctangle with new width height NewX and NewY and use it as cropRect
mihirpmehta
@mihirpmehta,your above method is just drawing the mirror image of original image in the cropRect rectangle. It could not been processed the UIImage.
Rajendra Bhole
sorry... change the XY coordinate to X+(Oldwidth/4) and y+(OldHeight/4)... my mistake..
mihirpmehta
@mihirpmehta,I think i need to crop the image using pixel processing from your given co-ordinate.But how it does?
Rajendra Bhole
A: 

I was looking for a way to get an arbitrary rectangular crop (ie., sub-image) of a UIImage.

Most of the solutions I tried do not work if the orientation of the image is anything but UIImageOrientationUp.

For example:

http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

Typically if you use your iPhone camera, you will have other orientations like UIImageOrientationLeft, and you will not get a correct crop with the above. This is because of the use of CGImageRef/CGContextDrawImage which differ in the coordinate system with respect to UIImage.

The code below uses UI* methods (no CGImageRef), and I have tested this with up/down/left/right oriented images, and it seems to work great.


// get sub image
- (UIImage*) getSubImageFrom: (UIImage*) img WithRect: (CGRect) rect {

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // translated rectangle for drawing sub image 
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height);

    // clip to the bounds of the image context
    // not strictly necessary as it will get clipped anyway?
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));

    // draw image
    [img drawInRect:drawRect];

    // grab image
    UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return subImage;
}

M-Y