如何实现图像周围的边框

发布于 2024-10-05 10:30:43 字数 130 浏览 1 评论 0原文

我喜欢这个(http://shakeitphoto.com/)应用程序在图像周围放置边框的方式。我想在我的应用程序中做类似的事情,但不确定我应该如何去做。

关于如何给定一个 UIImage 我可以在它周围包裹一个框架有什么想法吗?

I like the way this (http://shakeitphoto.com/) application puts a border around the image.. i would like to do something similar in my application but not sure how should I go about doing it.

Any ideas on how given a UIImage can I wrap a frame around it?

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

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

发布评论

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

评论(1

赠意 2024-10-12 10:30:43

从该网站来看,您似乎想要一个带有阴影的边框。有 2 个合理的选择,如果您不关心阴影,则有 3 个选择。

如果您不关心阴影,您可以执行类似的操作

#import <QuartzCore/QuartzCore.h> // this should be at the top

// inside your view layout code
myImageView.layer.borderColor = [UIColor whiteColor].CGColor
myImageView.layer.borderWidth = 5;

,这将为您提供一个插入视图的 5 像素白色边框,分层在视图内容(例如图像)之上。它不会给你的是阴影。如果你想要阴影,还有另外两个选择。

您可以只创建一个包含边框和阴影的图像,而不包含其他内容。只需将其他所有内容设为 alpha 透明即可。然后,您可以简单地将此图像叠加在要显示的图像之上(使用 2 个图像视图,或者从 2 个图像视图中创建第三个图像)。这应该可以正常工作,但它不会缩放到不同的图像尺寸。对于链接的应用程序,图像大小始终相同,因此他们可以使用它。

另一种选择是简单地在新图像中的图像顶部绘制边框和阴影。这里有一些示例代码可以做到这一点 - 它创建一个与原始图像大小相同的新图像,但带有白色阴影边框:(

- (UIImage *)borderedImage:(UIImage *)image {
    // the following NO means the new image has an alpha channel
    // If you know the source image is fully-opaque, you may want to set that to YES
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    [image drawAtPoint:CGPointZero];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    const CGFloat shadowRadius = 5;
    CGContextSetShadowWithColor(ctx, 0, shadowRadius, [UIColor blackColor].CGColor);
    [[UIColor whiteColor] set];
    CGRect rect = (CGRect){CGPointZero, image.size};
    const CGFloat frameWidth = 5;
    rect = CGRectInset(rect, frameWidth / 2.0f, frameWidth / 2.0f);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    path.lineWidth = frameWidth;
    [path stroke];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // note: getting the new image this way throws away the orientation data from the original
    // You could create a third image by doing something like
    //   newImage = [UIImage imageWithCGImage:newImage.CGImage scale:newImage.scale orientation:image.orientation]
    // but I am unsure as to how orientation actually affects rendering (if at all)
    UIGraphicsEndImageContext();
    return newImage;
}

注意:此代码尚未编译,可能包含错误)

From that website, it appears you want a border with a shadow. There's 2 reasonable options, 3 if you don't care about the shadow.

If you don't care about the shadow, you can just do something like

#import <QuartzCore/QuartzCore.h> // this should be at the top

// inside your view layout code
myImageView.layer.borderColor = [UIColor whiteColor].CGColor
myImageView.layer.borderWidth = 5;

This will give you a 5-pixel white border inset into the view, layered on top of the view's contents (e.g. the image). What it won't give you is a shadow. If you want the shadow, there's 2 other options.

You could just create an image that includes the border and the shadow, and nothing else. Just make everything else alpha-transparent. Then you can simply layer this image on top of the one you want to display (either with 2 imageviews, or by creating a third image out of the 2). This should work fine, but it won't scale to different image sizes. In the case of the linked app, the image size is always the same so they could be using this.

The other option is to simply draw the border and shadow on top of your image in a new image. Here's a bit of sample code that will do this - it creates a new image the same size as your original, but with a white, shadowed border:

- (UIImage *)borderedImage:(UIImage *)image {
    // the following NO means the new image has an alpha channel
    // If you know the source image is fully-opaque, you may want to set that to YES
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    [image drawAtPoint:CGPointZero];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    const CGFloat shadowRadius = 5;
    CGContextSetShadowWithColor(ctx, 0, shadowRadius, [UIColor blackColor].CGColor);
    [[UIColor whiteColor] set];
    CGRect rect = (CGRect){CGPointZero, image.size};
    const CGFloat frameWidth = 5;
    rect = CGRectInset(rect, frameWidth / 2.0f, frameWidth / 2.0f);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    path.lineWidth = frameWidth;
    [path stroke];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // note: getting the new image this way throws away the orientation data from the original
    // You could create a third image by doing something like
    //   newImage = [UIImage imageWithCGImage:newImage.CGImage scale:newImage.scale orientation:image.orientation]
    // but I am unsure as to how orientation actually affects rendering (if at all)
    UIGraphicsEndImageContext();
    return newImage;
}

(note: this code has not been compiled and could contain bugs)

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