objective-c - 如何在iOS中将图像裁剪成多边形?

标签 objective-c ios

我想将 UIImageview 上的图像裁剪成任何形状

最佳答案

您设置了剪切路径,瞧:

// Load image thumbnail
    NSString *imageName = [self.picArray objectAtIndex:indexPath.row];
    UIImage *image = [UIImage imageNamed:imageName];        
    CGSize imageSize = image.size;
    CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);    

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    // Create the clipping path and add it
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:imageRect cornerRadius:5.0f];
    [path addClip];
    [image drawInRect:imageRect];
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

此代码加载图像并通过圆角矩形创建路径,结果是最终图像被裁剪,即圆角。 RoundedImage 就是结果。

关于objective-c - 如何在iOS中将图像裁剪成多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12280561/

相关文章:

ios - 如果文本是单个单词,UITextField adjustmentFontSizeToFitWidth 不起作用

ios - 插入一个数组会影响另一个数组吗?

ios - 将 Objective-C 框架转换为 Swift - 头文件仍然显示 Objective-C 函数

iphone - 由于 UIPopoverController,通用 iOS 应用程序在 iPhone/iTouch 3.1.3 上崩溃

ios - Delphi iOS7启动图像问题

ios - 使用 Monotouch 修剪视频失败并显示 "The operation could not be completed"

ios - NSTimer 给出不准确的结果

ios - 我可以知道 requireGestureRecognizerToFail 到底会做什么吗?

ios - 水平或垂直滚动​​,但不能同时滚动

objective-c - 无法从自定义类获取数据