【问题标题】:Slicing UIImage into triangles in iPhone app在 iPhone 应用程序中将 UIImage 切片成三角形
【发布时间】:2011-07-19 17:29:54
【问题描述】:

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

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

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;

}

感谢大家的帮助

【问题讨论】:

  • 你用什么把它们滑成正方形?
  • 这里有很多关于切片成正方形而不是三角形的好帖子。
  • 我只是想问一下你用的是哪一个正方形。我自己在这方面没有任何经验,所以我需要一个起点来提供帮助。此外,如果您提供此类其他详细信息,对其他人将更加有用。
  • 确定我已经更新了信息 - 感谢所有帮助

标签: iphone uiimage geometry slice image-clipping


【解决方案1】:

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

【讨论】:

  • 您需要为每个蒙版和每个图像尺寸创建灰度图像。如果输入图像始终具有相同的大小并且只有一组有限的掩码,那么此解决方案肯定比使用剪切路径更容易。
  • 嗯,确实没有考虑尺寸。但是看到他把它们从相机里拿出来,我想它们总是一样的分辨率;我想灰度图像可以轻松放大/缩小以适应更高分辨率的相机。
  • 如果你想要锐利的边缘,缩放时它们会变得模糊。而且我不确定摄像头的分辨率,AFAIK iDevices 上前置摄像头的分辨率低于背面摄像头的分辨率。但是,如果他将所有照片都缩放到相同的尺寸,那么遮罩图像就可以正常工作了。
  • 谢谢 - 我会看一下 - 但是三角形每次的大小都不一样。因此,也许我将不得不查看@DarkDust 提到的剪切路径
【解决方案2】:

您应该可以使用这种方法:使用UIGraphicsBeginImageContextWithOptions 创建一个新图像(您也可以使用等效的 CoreGraphics,但工作量更大),然后创建所需形状的贝塞尔路径,调用 [yourBezierPath addClip]然后在该上下文中绘制您的图像。然后,您可以使用UIGraphicsGetImageFromCurrentImageContext 获取生成的图像。之后请务必致电UIGraphicsEndImageContext()。请参阅 this question 以使用 UIGraphicsBeginImageContextWithOptions 创建图像。

【讨论】:

  • 这看起来是一个相当不错的建议 - 我明天会试一试并报告它的进展情况
猜你喜欢
  • 1970-01-01
  • 2012-06-09
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多