如何在iPhone中裁剪图像
我想要做与this问题中所述相同的操作。 在我的应用程序中,我想裁剪图像,就像我们在FaceBook中进行图像裁剪一样,任何人都可以通过优秀教程链接或任何示例代码指导我。我所提供的链接将完全描述我的要求。如何在iPhone中裁剪图像
您可以使用任何属性创建新图像。这是我的功能,女巫是这么做的。你只需要使用你自己的新图像参数。在我的情况下,图像没有被裁剪,我只是产生了一些效果,将像素从原始位置移动到另一个位置。但是,如果你与另一个高度和宽度初始化新的图像,你可以从你需要旧图像的像素的任何范围复制到新的一个:
-(UIImage *)Color:(UIImage *)img
{
int R;
float m_width = img.size.width;
float m_height = img.size.height;
if (m_width>m_height) R = m_height*0.9;
else R = m_width*0.9;
int m_wint = (int)m_width; //later, we will need this parameters in float and int. you may just use "(int)" and "(float)" before variables later, and do not implement another ones
int m_hint = (int)m_height;
CGRect imageRect;
//cheking image orientation. we will work with image pixel-by-pixel, so we need to make top side at the top.
if(img.imageOrientation==UIImageOrientationUp
|| img.imageOrientation==UIImageOrientationDown)
{
imageRect = CGRectMake(0, 0, m_wint, m_hint);
}
else
{
imageRect = CGRectMake(0, 0, m_hint, m_wint);
}
uint32_t *rgbImage = (uint32_t *) malloc(m_wint * m_hint * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_wint, m_hint, 8, m_wint *sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextTranslateCTM(context, 0, m_hint);
CGContextScaleCTM(context, 1.0, -1.0);
switch (img.imageOrientation) {
case UIImageOrientationRight:
{
CGContextRotateCTM(context, M_PI/2);
CGContextTranslateCTM(context, 0, -m_wint);
}break;
case UIImageOrientationLeft:
{
CGContextRotateCTM(context, - M_PI/2);
CGContextTranslateCTM(context, -m_hint, 0);
}break;
case UIImageOrientationUp:
{
CGContextTranslateCTM(context, m_wint, m_hint);
CGContextRotateCTM(context, M_PI);
}
default:
break;
}
CGContextDrawImage(context, imageRect, img.CGImage);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
//here is new image. you can change m_wint and m_hint as you whant
uint8_t *result = (uint8_t *) calloc(m_wint * m_hint * sizeof(uint32_t), 1);
for(int y = 0; y < m_hint; y++) //new m_hint here
{
float fy=y;
double yy = (m_height*( asinf(m_height/(2*R))-asin(((m_height/2)-fy)/R) ))/
(2*asin(m_height/(2*R))); // (xx, yy) - coordinates of pixel of OLD image
for(int x = 0; x < m_wint; x++) //new m_wint here
{
float fx=x;
double xx = (m_width*( asin(m_width/(2*R))-asin(((m_width/2)-fx)/R) ))/
(2*asin(m_width/(2*R)));
uint32_t rgbPixel=rgbImage[(int)yy * m_wint + (int)xx];
int intRedSource = (rgbPixel>>24)&255;
int intGreenSource = (rgbPixel>>16)&255;
int intBlueSource = (rgbPixel>>8)&255;
result[(y * (int)m_wint + x) * 4] = 0;
result[(y * (int)m_wint + x) * 4 + 1] = intBlueSource;
result[(y * (int)m_wint + x) * 4 + 2] = intGreenSource;
result[(y * (int)m_wint + x) * 4 + 3] = intRedSource;
}
}
free(rgbImage);
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result, m_wint, m_hint, 8, m_wint * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast ); //new m_wint and m_hint as well
CGImageRef image1 = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *resultUIImage = [UIImage imageWithCGImage:image1];
CGImageRelease(image1);
@try {
free(result);
}
@catch (NSException * e) {
NSLog(@"proc. Exception: %@", e);
}
return resultUIImage;
}
的CGRect rectImage = CGRectMake(p1.x,p1.y ,p2.x - p1.x,p4.y - p1.y);
//Create bitmap image from original image data,
//using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([imageForCropping CGImage], rectImage);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(p1.x, p1.y,p2.x-p1.x p4.y-p1.y)];
imageView1.image = croppedImage;
[self.view addSubview:imageView1];
CGImageRelease(imageRef);
这里imageForcropping是你想要裁剪的图像..和裁剪图像你想裁剪哪个部分.. – 2012-05-16 07:43:52
任何想法如何当图像旋转时(即从iPhone摄像头拍摄时)使这项工作? –
我想你想裁剪图像当旋转图像! – 2012-12-18 04:59:51
定点感谢您的答复,但我的事情你没有得到我的问题,我想我的裁剪图像对此我用代码'的CGRect cropRect = CGRectMake(100,100,200,200)这行做; (http://img192.imageshack.us/)这个裁剪是硬编码的,我希望用户调整这个[链接]中显示的图像链接的Rect(http://img192.imageshack.us/)。 img192/8930/customcropbox.jpg) –
所以......你只是想让用户界面,选择rect裁剪图像?这很简单:只需实施触摸即可。与阿尔法0.7的4个blak酒吧将静音区域将被蜷缩。您可以处理两个触摸:根据这些坐标,并且知道它们位于矩形对角线的不同侧,可以轻松计算出4个黑色条的大小。只是在一举一动中改变泰姆。或者只需一次触摸:您将需要4个空格(即蓝色圆圈)。跟踪接触,并有移动tham – SentineL
感谢SentineL为你的反应和方式,你指导我:) –