2016-03-20 32 views

cevap

7
Sen, bir görüntü içeriğini oluşturmak kısaltıldı görüntüyü çizmek ve daha sonra felç yolda için QuartzCore işlevlerini kullanabilirsiniz

:

- (UIImage *)imageWithBorderAndRoundCornersWithImage:(UIImage *)image lineWidth:(CGFloat)lineWidth cornerRadius:(CGFloat)cornerRadius { 
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale); 
    CGRect rect = CGRectZero; 
    rect.size = image.size; 
    CGRect pathRect = CGRectInset(rect, lineWidth/2.0, lineWidth/2.0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:cornerRadius]; 

    CGContextBeginPath(context); 
    CGContextAddPath(context, path.CGPath); 
    CGContextClosePath(context); 
    CGContextClip(context); 

    [image drawAtPoint:CGPointZero]; 

    CGContextRestoreGState(context); 

    [[UIColor whiteColor] setStroke]; 
    path.lineWidth = lineWidth; 
    [path stroke]; 

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

    return finalImage; 
} 

alır:

without border

Ve markaları :

enter image description here

-1

UIImageView uygulamasının bunu basit bir şekilde yapmanız gerekir. Yuvarlatılmış (Köşe Yarıçapı) ve kenarlık kalınlık/renk UIView CALayer'in tipik özellikleridir. En basit yol, bunu uygulamak için bir UIImageView veya belki başka bir UIView türüne sahip olmaktır.

Böyle bir şey işi yapabilir.

let myImg = UIImage(named: "some_image.png") 
let imgView = UIImageView(image: myImg) 
imgView.clipsToBounds = true 
imgView.layer.cornerRadius = 0.5*imgView.frame.width 
imgView.layer.borderWidth = 5.0 //Or some other value 
imgView.layer.borderColor = UIColor.whiteColor().CGColor 
+0

Bu, görüntüyü, görüntü görünümünün boyutuna ve bunu yaptığınız aygıtın ölçeğine göre yeniden boyutlandıracaktır. İstenilen etki bu olursa, bu iyi olur, ancak OP bunun farkında olmalıdır. Ayrıca, bu tekniğin bazen yuvarlatılmış köşelerin dış kenarları boyunca istenmeyen yapay dokulara neden olabileceğini de unutmayın. Bunu, görüntü görünümünü maskeleyerek çözebilirsiniz. Bkz. Http://stackoverflow.com/a/30125297/1271826. – Rob

İlgili konular