如何在iphone中裁剪特定图像
Posted
技术标签:
【中文标题】如何在iphone中裁剪特定图像【英文标题】:How to crop a particular image in iphone 【发布时间】:2011-08-10 06:22:47 【问题描述】:我正在开发相机应用程序,用户正在拍照,但我想裁剪该图像中的任何位置并将其发送到服务器。我该怎么做?
【问题讨论】:
你应该接受一些答案...... 你想要这个....link 看这个。我想你想要同样的link 【参考方案1】:查看此链接了解详情:
http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/
基本代码:
- (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;
【讨论】:
感谢@robin 回答我。请让我知道我把他的代码放在哪里。 好的,我想在 viewdidload 方法上调用它如何调用它。我正在写 [self imageToCrop];它给出了错误。请告诉我怎么写。 我的图片尺寸是150 * 120(宽*高) 您是否在视图中的某个位置显示此图像。你也可以告诉我你打算如何发送这张图片。以上是关于如何在iphone中裁剪特定图像的主要内容,如果未能解决你的问题,请参考以下文章
如何在 OpenCV c++ 中从图像中裁剪特定的矩形部分(ROI)