2013-12-18 88 views
0

試圖讓圓角和中風, 的圖像,但也有一些是我做錯了,因爲應用程序在此掛起:圖像的圓角和邊框

- (UIImage *)roundedCornerImage:(NSInteger)radius{ 
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0); 

    CGRect box = CGRectInset((CGRect){CGPointZero, self.size}, self.size.width * 0.9f, self.size.height * 0.9f); 
    UIBezierPath *ballBezierPath = [UIBezierPath bezierPathWithOvalInRect:box]; 

    [[UIColor blackColor] setStroke]; 
    [ballBezierPath setLineWidth:4.0]; 
    [ballBezierPath stroke]; 

    [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, self.size} 
           cornerRadius:radius]addClip]; 

    [self drawInRect:(CGRect){CGPointZero, self.size}]; 
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return result; 
} 

回答

1

忘掉搞亂圖層,它是滾動視圖中的性能殺手。只是生成一個新的圖像,而不是:

+(UIImage *)makeRoundedImage:(UIImage *)image withRadius:(CGFloat)radius 
{ 
    CGRect itemFrame = CGRectMake(0, 0, radius*2, radius*2); 

    // The source image 
    UIImageView *imageView = [UIImageView new]; 
    imageView.frame = itemFrame; 
    imageView.contentMode = UIViewContentModeScaleToFill; 
    [imageView setImage:image]; 
    imageView.layer.cornerRadius = radius; 
    imageView.layer.masksToBounds = YES; 

    // Make an image of our client item 
    UIGraphicsBeginImageContextWithOptions(itemFrame.size, NO, 0.0); 
    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Fini 
    return returnImage; 
} 
+0

感謝您標記解決方案,即使您的問題很久以前才被問到。 – GoldenJoe

2

只需使用

[imageView.layer setCornerRadius:5.0f]; 
[imageView.layer setBorderWidth:5.0f]; 

不要忘記@import Quartzcore;

+0

不,我不想使用CA,我想要繪製邊框。 – Shmidt

+0

然後將圖層渲染成位圖。無論如何,它一直通過Quartz,不需要過度複雜的事情。 – JustSid