在 iPhone 应用程序中将 UIImage 切成三角形

发布于 2024-10-24 16:40:20 字数 2192 浏览 1 评论 0 原文

我正在开发一个 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;

}

感谢您的所有帮助

I am working on a iPhone app where I take a picture with the camera. Hereafter it should be possible to slice the image in various shapes - most important triangles. Can anyone give me a point in the right direction or examples etc. I have made it possible to slice into squares but not triangles.

(Updated)For creating the squared slices I used the following code snippets

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

All help is appreciated

Regards

SOLVED:

I went with the method of masking the uiimage. So the strategy is that 1. Take picture from camera and scale. 2. Let the user draw a figure in a uiimageview and fill it with black color and the create a UIImage from this. 3. Mask the image from the camera with the user generated image.
For masking the image I use the following method from 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;

}

Thank you for all the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-10-31 16:40:20

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

Try something like this:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
Simply make a black image as your mask and apply it to the image, if i get this explanation right.

可爱咩 2024-10-31 16:40:20

您应该能够使用这种方法:使用 UIGraphicsBeginImageContextWithOptions (您也可以使用 CoreGraphics 等效项,但需要更多工作),然后创建所需形状的贝塞尔曲线路径,调用 [yourBezierPath addClip],然后在该上下文中绘制图像。然后,您可以使用 UIGraphicsGetImageFromCurrentImageContext。请务必随后调用 UIGraphicsEndImageContext()。有关使用 UIGraphicsBeginImageContextWithOptions 创建图像的信息,请参阅此问题

You should be able to use this approach: Create a new image using UIGraphicsBeginImageContextWithOptions (you could also use the CoreGraphics equivalent but is much more work), then create a bezier path of the shape you want, call [yourBezierPath addClip] and then draw your image in that context. You can then get the resulting image using UIGraphicsGetImageFromCurrentImageContext. Be sure to call UIGraphicsEndImageContext() afterwards. See this question for creating an image with UIGraphicsBeginImageContextWithOptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文