Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (UIImage *)drawImageWithIdentifier:(NSString *)currencyIdentifier inView:(UIImageView *)imageView {
- ColorTable *colorTable = [[ColorTable alloc] init];
- CGFloat contentInset = imageView.frame.size.height/4;
- UIView *backgroundView = [[UIView alloc] initWithFrame:self.imageView.bounds];
- backgroundView.backgroundColor = [colorTable colorWithID:currencyIdentifier];
- UIImage *currencyImage = [UIImage imageNamed:@"ethereum_icon.png"];
- UIGraphicsBeginImageContextWithOptions(backgroundView.bounds.size, NO, [UIScreen mainScreen].scale);
- [[UIBezierPath bezierPathWithRoundedRect:backgroundView.bounds cornerRadius:backgroundView.frame.size.height/2] addClip];
- [backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
- CGFloat aspectRatio = currencyImage.size.width/currencyImage.size.height;
- if (aspectRatio < 1) {
- //vertical image
- CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio , backgroundView.frame.size.height - 2*contentInset);
- CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
- CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
- [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
- }
- else if (aspectRatio > 1) {
- //horizontal image
- CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio, backgroundView.frame.size.height - 2*contentInset);
- CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
- CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
- [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
- }
- else if (aspectRatio == 1) {
- //square image
- CGRect imageRect = CGRectMake(0, 0, backgroundView.frame.size.height - 2*contentInset, backgroundView.frame.size.height - 2*contentInset);
- CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
- CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
- [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
- }
- UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return finalImage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement