2012-10-26 3 views
4

클리핑을 통해 둥글게하려는 정사각형 이미지가 40x40이지만 이미지 주위에 검정색 5 픽셀 테두리를 넣을 수도 있습니다.정사각형 이미지를 원으로 마스킹하고 이미지 주위에 검은 색 테두리를 넣을 수 있습니다.

나는 그 지금 라운드

UIImage *image = self.imageView.image; 
     CGSize imageSize = image.size; 
     CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height); 

     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); 
     // Create the clipping path and add it 
     UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:imageRect]; 
     [path addClip]; 


     [image drawInRect:imageRect]; 
     UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     self.imageView.image = roundedImage; 

있도록 사각형 이미지를 마스킹 한 다음이하지만 지금은 주위에 둥근 테두리를 추가해야합니다. 새로운 경로가 필요합니까? 아니면 위의 코드에서 하나를 택할 수 있습니까?

+1

[imageView.layer setBorderColor : [[UIColor blackColor ] CGColor]]; [imageView.layer setBorderWidth : 1.0]; [imageView.layer setCornerRadius : 10.0f]; 이런 식으로 경계를 설정하면 어떻게 될까요? 작동 되나요? – iDev

+0

이 코드는 잘린 이미지가 아닌'imageView' 레이어에 테두리를 추가합니다. – atxe

+0

imgView.clipsToBounds = YES로 설정하면 작동합니다. – Allfocus

답변

12

(당신이 원하는 색상과 스트로크 폭) 코드에서 다음 세 줄을 추가

CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]); 
[path setLineWidth:50.0f]; 
[path stroke]; 

것은 그래서됩니다 :

UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
// Create the clipping path and add it 
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:imageRect]; 
[path addClip]; 
[image drawInRect:imageRect]; 

CGContextSetStrokeColorWithColor(ctx, [[UIColor greenColor] CGColor]); 
[path setLineWidth:50.0f]; 
[path stroke]; 

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

self.imageView.image = roundedImage; 
+0

완벽한! 그러나 50.0f 대신 5.0f로 설정하십시오. – jdog

관련 문제