2011-08-10 27 views
0

我正在处理Camera Application用户正在拍摄照片,但我想裁剪该图像中的任何位置并将其发送到服务器。我怎样才能做到这一点?如何在iphone中裁剪特定图像

+1

你应该接受一些答案....... – Robin

+0

ü要这个...... [链接](http://img192.imageshack.us/img192/8930/customcropbox.jpg) – avinash

+0

看到这个。我想你想要同样的[链接](http://stackoverflow.com/questions/1971542/iphone-how-do-you-make-a-resizable-rectangle-for-cropping-images) – avinash

回答

3

退房此链接的详细信息:

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

Basic代码:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 
    //create a context to do our clipping in 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CGContextClipToRect(currentContext, clippedRect); 

    //create a rect equivalent to the full size of the image 
    //offset the rect by the X and Y we want to start the crop 
    //from in order to cut off anything before them 
    CGRect drawRect = CGRectMake(rect.origin.x * -1, 
           rect.origin.y * -1, 
           imageToCrop.size.width, 
           imageToCrop.size.height); 

    //draw the image to our clipped context using our offset rect 
    CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage); 

    //pull the image from our cropped context 
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    //Note: this is autoreleased 
    return cropped; 
} 
2

我想我可以提供一个更好的解决方案到大量的代码。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // do something...... 
    UIImage *croppedImage = [self imageByCropping:[UIImage imageNamed:@"SomeImage.png"] toRect:CGRectMake(10, 10, 100, 100)]; 
} 
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
    UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
    return cropped; 
} 
+0

感谢@robin回答我。请让我知道我把他的代码放在哪里。 – pinku

+0

好的我想调用它在viewdidload方法如何调用此。我正在写[self imageToCrop];它会给出错误。请让我知道如何写。 – pinku

+0

好的,谢谢我已经删除。 – pinku