简体   繁体   English

如何在iOS中为UIImageView提供3D边框

[英]How to provide a 3D border to a UIImageView in iOS

I want to provide a 3D just like the image posted here. 我想提供3D效果,就像这里发布的图片一样。 在此处输入图片说明

How can I do that? 我怎样才能做到这一点? Any suggestions would be appreciated. 任何建议,将不胜感激。 Thanks in advance. 提前致谢。

Before Shah's answer ![enter image description here][2] 在Shah回答之前![在此处输入图片描述] [2]

After his answer ![enter image description here][3] 在他回答之后![在此处输入图片描述] [3]

using coreGraphics use shadow. 使用coreGraphics使用阴影。 sample code for shadow is. 阴影的示例代码是。

one imageview span entire view on viewcontroller. 一个imageview涵盖了viewcontroller上的整个视图。

in .h file @property CGContextRef context; 在.h文件@property CGContextRef上下文中;

    #define radians(degrees) (degrees * M_PI/180)
      -(void) JustForYouShadow
 {
UIImage *image=[UIImage imageNamed:@"button.jpg"];
UIGraphicsBeginImageContext(self.view.frame.size);

[self.imgView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
    //shadow for iBeacon
     CGSize  myShadowOffset = CGSizeMake (0.9,  15);
     CGContextSaveGState(context);
     CGContextSetShadow (context, myShadowOffset, 10);

     CGRect rectangle = CGRectMake(10,10,150,150);

     CGContextTranslateCTM( context, 90, 80 ) ;
     CGContextRotateCTM(context, radians(30));

     CGContextDrawImage(context, rectangle, image.CGImage);
     CGContextStrokePath(context);
     CGContextRestoreGState(context);
//get line on image
self.imgView.image = UIGraphicsGetImageFromCurrentImageContext();
[self.imgView setAlpha:1.0];
UIGraphicsEndImageContext();

 }

在此处输入图片说明

it's running fine, this is screen snap of simulator. 运行正常,这是模拟器的屏幕快照。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM