我想在汽车周围画一个边框:
我怎样才能达到这个效果?
目前,我已经以这种方式测试了CoreGraphics,但没有取得好成绩:
// load the image UIImage *img = carImage; UIGraphicsBeginImageContext(img.size); CGContextRef context = UIGraphicsGetCurrentContext(); [[UIcolor redcolor] setFill]; CGContextTranslateCTM(context,img.size.height); CGContextScaleCTM(context,1.0,-1.0); CGContextSetBlendMode(context,kCGBlendModenormal); CGRect rect = CGRectMake(0,img.size.wIDth * 1.1,img.size.height*1.1); CGContextDrawImage(context,rect,img.CGImage); CGContextClipToMask(context,img.CGImage); CGContextAddRect(context,rect); CGContextDrawPath(context,kCGPathFill); // generate a new UIImage from the graphics context we drew onto UIImage *coloredimg = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext();
有帮助吗?谢谢.
解决方法 这是我做的:我在Swift中做过它只是为了在游乐场中检查它,认为你可以轻松地将它转换为Objective-C:
import UIKitfunc drawOutlIE(#image:UIImage,color:UIcolor) -> UIImage{ var newImageKoef:CGfloat = 1.08 var outlinedImageRect = CGRect(x: 0.0,y: 0.0,wIDth: image.size.wIDth * newImageKoef,height: image.size.height * newImageKoef) var imageRect = CGRect(x: image.size.wIDth * (newImageKoef - 1) * 0.5,y: image.size.height * (newImageKoef - 1) * 0.5,wIDth: image.size.wIDth,height: image.size.height) UIGraphicsBeginImageContextWithOptions(outlinedImageRect.size,false,newImageKoef) image.drawInRect(outlinedImageRect) var context = UIGraphicsGetCurrentContext() CGContextSetBlendMode(context,kCGBlendModeSourceIn) CGContextSetFillcolorWithcolor(context,color.CGcolor) CGContextFillRect(context,outlinedImageRect) image.drawInRect(imageRect) var newImage = UIGraphicsGetimageFromCurrentimageContext() UIGraphicsEndImageContext() return newImage}var imageIn = UIImage(named: "158jM")var imageOut = drawOutlIE(image: imageIn,UIcolor.redcolor())
那么它是怎样工作的?
>我们创建干净的上下文(又名画布),其尺寸略大于原始图像(用于轮廓)
>我们在整个画布上绘制图像
>我们用颜色填充该图像
>我们在顶部绘制较小的图像
您可以更改轮廓大小更改此属性:var newImageKoef:CGfloat = 1.08
这是我在 *** 场上的结果
总结以上是内存溢出为你收集整理的ios – 在UIImageView的内容周围绘制边框全部内容,希望文章能够帮你解决ios – 在UIImageView的内容周围绘制边框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)