1

我正在开发一个用相机拍照的 iPhone 应用程序。此后应该可以将图像切成各种形状 - 最重要的三角形。谁能给我一个正确的方向或示例等。我已经可以切成正方形但不能切成三角形。

(更新)为了创建平方切片,我使用了以下代码片段

CGRect ClippedRect= CGRectMake(0, 150, 320.0, 230.0);//example numbers
CGImageRef imageRef = CGImageCreateWithImageInRect([OriginalUIImage CGImage], ClippedRect);
UIImage *resultUIImage=[[UIImage alloc]initWithCGImage:imageRef];

感谢所有帮助

问候

解决了:

我采用了屏蔽uiimage的方法。所以策略是 1. 从相机和比例尺上拍照。2. 让用户在 uiimageview 中绘制一个图形并用黑色填充它,然后从中创建一个 UIImage。3. 使用用户生成的图像屏蔽来自相机的图像。为了屏蔽图像,我使用来自http://www.developers-life.com/resize-and-mask-an-image.html的以下方法

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

 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

 UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
 CGImageRef maskImageRef = [maskImage CGImage];

 // create a bitmap graphics context the size of the image
 CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);


 if (mainViewContentContext==NULL)
      return NULL;

 CGFloat ratio = 0;

 ratio = maskImage.size.width/ image.size.width;

 if(ratio * image.size.height < maskImage.size.height) {
      ratio = maskImage.size.height/ image.size.height;
 } 

 CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
 CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};


 CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
 CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);


 // Create CGImageRef of the main view bitmap content, and then
 // release that bitmap context
 CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
 CGContextRelease(mainViewContentContext);

 UIImage *theImage = [UIImage imageWithCGImage:newImage];

 CGImageRelease(newImage);

 // return the image
 return theImage;

}

谢谢大家的帮助

4

2 回答 2

1

尝试这样的事情:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
只需制作一个黑色图像作为您的蒙版并将其应用于图像,如果我得到这个解释正确。

于 2011-03-21T15:11:37.620 回答
0

您应该可以使用这种方法:使用UIGraphicsBeginImageContextWithOptions创建一个新图像(您也可以使用 CoreGraphics 等效项,但工作量更大),然后创建所需形状的贝塞尔路径,调用[yourBezierPath addClip]然后绘制您的在这种情况下的形象。然后,您可以使用UIGraphicsGetImageFromCurrentImageContext获取生成的图像。以后一定要打电话UIGraphicsEndImageContext()。请参阅此问题以使用UIGraphicsBeginImageContextWithOptions.

于 2011-03-21T15:18:30.067 回答