모든 UIImageViews에 둥근 모서리 추가
내 프로젝트의 모든 UIImageView에 둥근 모서리를 추가하고 싶습니다. 이미 코드가 작동하고 있지만 모든 이미지에 적용해야합니다. 이것을 추가하려면 UIImageView를 하위 클래스로 만들어야합니까? 그렇다면 누군가가 이것을 수행하는 방법에 대한 몇 가지 지침을 줄 수 있습니까?
다음은 코드입니다.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *mainpath = [[NSBundle mainBundle] bundlePath];
welcomeImageView.image = [UIImage imageWithContentsOfFile:[mainpath stringByAppendingString:@"/test.png"]];
welcomeImageView.layer.cornerRadius = 9.0;
welcomeImageView.layer.masksToBounds = YES;
welcomeImageView.layer.borderColor = [UIColor blackColor].CGColor;
welcomeImageView.layer.borderWidth = 3.0;
CGRect frame = welcomeImageView.frame;
frame.size.width = 100;
frame.size.height = 100;
welcomeImageView.frame = frame;
}
클래스를 하위 클래스로 만드는 대체 방법 인 UIImage에 대한 범주를 사용할 수 있으며 때로는 작은 변경에 더 쉽게 적용 할 수 있습니다.
예를 들어 둥근 모서리 속성이 설정된 UIImage를 반환하는 메서드를 추가합니다.
+(UIImage *)imageWithContentsOfFile:(NSString *)file cornerRadius:(NSInteger)...
Objective-c 카테고리에 대한 자세한 정보는 http://macdevelopertips.com/objective-c/objective-c-categories.html 에서 찾을 수 있습니다 .
이것을 확인하십시오 -UIImage의 둥근 모서리
레이어 수정이 가장 좋은 방법 인 것 같습니다.
UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
서브 클래 싱 대신 UIImageView 및 CALayer의 간단한 카테고리를 통해 더 강력한 기능을 얻을 수 있습니다.
다음과 같이 UIImageView에 범주를 만듭니다.
- (void)maskRoundCorners:(UIRectCorner)corners radius:(CGFloat)radius {
// To round all corners, we can just set the radius on the layer
if ( corners == UIRectCornerAllCorners ) {
self.layer.cornerRadius = radius;
self.layer.masksToBounds = YES;
} else {
// If we want to choose which corners we want to mask then
// it is necessary to create a mask layer.
self.layer.mask = [CALayer maskLayerWithCorners:corners radii:CGSizeMake(radius, radius) frame:self.bounds];
}
}
CALayer에서 카테고리 메소드를 호출합니다.
+ (id)maskLayerWithCorners:(UIRectCorner)corners radii:(CGSize)radii frame:(CGRect)frame {
// Create a CAShapeLayer
CAShapeLayer *mask = [CAShapeLayer layer];
// Set the frame
mask.frame = frame;
// Set the CGPath from a UIBezierPath
mask.path = [UIBezierPath bezierPathWithRoundedRect:mask.bounds byRoundingCorners:corners cornerRadii:radii].CGPath;
// Set the fill color
mask.fillColor = [UIColor whiteColor].CGColor;
return mask;
}
따라서 UIRectCorner
모서리의 모든 조합 (참조 ) 을 둥글게 할 수 있습니다 UITableView
. 이는 그룹 스타일로 이미지를 배치하려는 경우 특히 유용합니다 . 그러나이 작업을 수행 할 때 한 가지주의 사항이 있습니다. 서브 클래 싱하지 않았기 때문에에 UIImageView
코드를 삽입 할 수 없습니다. layoutSubviews
즉, 마스크 레이어가 올바르지 않을 수 있습니다. 실제로 셀을 구성 할 때 category 메서드를 호출 할 때 이미지보기의 경계가 설정되지 않습니다. 따라서 둥근 모서리를 추가하기 전에 이미지보기의 경계가 설정되었는지 확인해야합니다 (사용하는 경우 제외 UIRectCornersAllCorners
).
다음은이를 수행하는 코드입니다.
// Perform corner rounding
UIRectCorner corners = !UIRectCornerAllCorners;
if (indexPath.row == 0)
corners = UIRectCornerTopLeft;
if (indexPath.row == numberOfRowsInTheTable)
corners |= UIRectCornerBottomLeft;
if (corners > 0) {
cell.imageView.bounds = CGRectMake(0.f, 0.f, [self.tableView rowHeight], [self.tableView rowHeight]);
[cell.imageView maskRoundCorners:corners radius:10.f];
} else {
[cell.imageView removeRoundCornersMask];
}
I have another category which removes rounded corners - all that does is remove any masks and set the cornerRadius
to 0.
Yes, you should subclass UIImageView, and use your custom subclass throughout your project.
You can subclass UIImageView and then if you implement its setNeedsDisplay method the round corners will work on subclasses. (don't forget to import QuartzCore)
-(void)setNeedsDisplay {
self.layer.cornerRadius = 5;
self.layer.masksToBounds = YES;
[self.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[self.layer setBorderWidth: 2.0];
}
Try this,
coverImage.image = [UIImage imageWithContentsOfFile:@"coverImage.png"];
coverImage.layer.masksToBounds = YES;
coverImage.layer.cornerRadius = 10.0;
coverImage.layer.borderWidth = 1.0;
coverImage.layer.borderColor = [[UIColor brown] CGColor];
this may help you.
참고URL : https://stackoverflow.com/questions/2171506/add-rounded-corners-to-all-uiimageviews
'Programing' 카테고리의 다른 글
nginx.conf를 편집하여 파일 크기 업로드를 늘리는 방법 (0) | 2020.10.12 |
---|---|
Xcode 9 : Swift 3.1로 컴파일 된 모듈은 Swift 4.0에서 가져올 수 없습니다. (0) | 2020.10.12 |
iOS에서 CSS 오버플로가있는 스크롤 막대를 얻는 방법 (0) | 2020.10.12 |
==가 null 값에 대해 true를 반환 할 때> =가 false를 반환하는 이유는 무엇입니까? (0) | 2020.10.12 |
배열을 ASP.NET MVC 컨트롤러 작업 매개 변수로 받아들이는 방법은 무엇입니까? (0) | 2020.10.12 |