在UIImage上绘制边框和阴影

问题描述 投票:0回答:2

我已经能够成功地重新绘制带有边框的UIImage,但不能重新绘制带有边框+阴影的UIImage。下面的代码成功绘制了带有白色边框的图像,但是我无法弄清楚如何在边框下添加阴影。非常感谢您的帮助!

- (UIImage *)generatePhotoFrameWithImage:(UIImage *)image {
CGSize newSize = CGSizeMake(image.size.width + 50, image.size.height + 60);
UIGraphicsBeginImageContext(newSize);
CGRect rect = CGRectMake(25, 35, image.size.width, image.size.height);
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextStrokeRectWithWidth(context, CGRectMake(25, 35, image.size.width, image.size.height), 50);

//CGContextSetShadowWithColor(context, CGSizeMake(0, -60), 5, [UIColor blackColor].CGColor);

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

return result; }

PS:我没有使用UIImageView,因为我需要保留完整的图像质量。

iphone objective-c ios uiimageview uiimage
2个回答
9
投票

您在划边框之前需要调用CGContextSetShadowWithColor,以便边框会投射阴影。

如果您不希望边框在图像上投射阴影,则需要设置一个透明层,在其中绘制图像和边框。您可以在Quartz 2D Programming Guide中阅读有关透明层的信息。

如果要保持图像质量,则需要使用UIGraphicsBeginImageContextWithOptions并传递原始图像的比例尺。

- (UIImage *)generatePhotoFrameWithImage:(UIImage *)image {
    CGSize newSize = CGSizeMake(image.size.width + 50, image.size.height + 60);
    CGRect rect = CGRectMake(25, 35, image.size.width, image.size.height);

    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextBeginTransparencyLayer(context, NULL); {
            [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

            CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
            CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5, [UIColor blackColor].CGColor);
            CGContextStrokeRectWithWidth(context, CGRectMake(25, 35, image.size.width, image.size.height), 50);
        } CGContextEndTransparencyLayer(context);
    }
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

4
投票

感谢罗布为我指出正确的方向。这是最终的代码,它在图像周围绘制白色边框,然后在边框外部绘制浅灰色阴影

- (UIImage *)generatePhotoStackWithImage:(UIImage *)image {
    CGSize newSize = CGSizeMake(image.size.width + 70, image.size.height + 70);
    CGRect rect = CGRectMake(25, 25, image.size.width, image.size.height);

    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); {
    CGContextRef context = UIGraphicsGetCurrentContext();
        //Shadow
        CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 10, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f].CGColor);

        CGContextBeginTransparencyLayer (context, NULL);
        //Draw
        [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
        CGContextStrokeRectWithWidth(context, rect, 40);
        CGContextEndTransparencyLayer(context);
    }

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

    return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.