iOS 프로그래밍/iOS
iOS)이미지에 색 넣기
myoungsc.dev
2016. 3. 3. 23:11
이미지에 원하는 색으로 색칠을 해주는 코드입니다.
같은 아이콘이지만 여러가지 색으로 바꿔줘야 할때 유용 할 것 같습니다.
(Objective-C 코드)
1 2 3 4 5 6 7 8 9 10 | UIImage *image = [UIImage imageNamed:@“your_img_name”]; CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale); CGContextRef c = UIGraphicsGetCurrentContext(); [image drawInRect:rect]; CGContextSetFillColorWithColor(c, [UIColor blackColor].CGColor); CGContextSetBlendMode(c, kCGBlendModeSourceAtop); CGContextFillRect(c, rect); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); | cs |
(Swift 코드)
1 2 3 4 5 6 7 8 9 10 | let image = UIImage(named: "your_img_name") let rect = CGRectMake(0, 0, image!.size.width, image!.size.height) UIGraphicsBeginImageContextWithOptions(rect.size, false, image!.scale) let c : CGContextRef = UIGraphicsGetCurrentContext()! image?.drawInRect(rect) CGContextSetFillColorWithColor(c, UIColor.blackColor().CGColor) CGContextSetBlendMode(c, CGBlendMode.SourceAtop) CGContextFillRect(c, rect) let resultImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() | cs |