4

クリッピングで丸くしたい40x40の正方形の画像がありますが、画像の周りに黒い5ピクセルの境界線も付けます。

私は正方形の画像をマスキングしている次のものを持っているので、今は丸いです

 UIImage *image = self.imageView.image;
        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 bezierPathWithOvalInRect:imageRect];
        [path addClip];


        [image drawInRect:imageRect];
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        self.imageView.image = roundedImage;

しかし、今度はその周りに丸い境界線も追加する必要があります. 新しいパスが必要ですか、それとも上記のコードの 1 つに追加できますか?

4

1 に答える 1

12

次の 3 行をコードに追加します (任意の色とストローク幅で)。

CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]);
[path setLineWidth:50.0f];
[path stroke];

したがって、次のようになります。

UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Create the clipping path and add it
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:imageRect];
[path addClip];
[image drawInRect:imageRect];

CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]);
[path setLineWidth:50.0f];
[path stroke];

UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.imageView.image = roundedImage;
于 2012-10-26T06:25:18.493 に答える