Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (UIImage *)drawImageWithIdentifier:(NSString *)currencyIdentifier inView:(UIImageView *)imageView {
  2.    
  3.     ColorTable *colorTable = [[ColorTable alloc] init];
  4.    
  5.     CGFloat contentInset = imageView.frame.size.height/4;
  6.    
  7.     UIView *backgroundView = [[UIView alloc] initWithFrame:self.imageView.bounds];
  8.     backgroundView.backgroundColor = [colorTable colorWithID:currencyIdentifier];
  9.    
  10.     UIImage *currencyImage = [UIImage imageNamed:@"ethereum_icon.png"];
  11.    
  12.     UIGraphicsBeginImageContextWithOptions(backgroundView.bounds.size, NO, [UIScreen mainScreen].scale);
  13.    
  14.     [[UIBezierPath bezierPathWithRoundedRect:backgroundView.bounds cornerRadius:backgroundView.frame.size.height/2] addClip];
  15.    
  16.     [backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
  17.    
  18.     CGFloat aspectRatio = currencyImage.size.width/currencyImage.size.height;
  19.    
  20.     if (aspectRatio < 1) {
  21.         //vertical image
  22.         CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio , backgroundView.frame.size.height - 2*contentInset);
  23.         CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
  24.         CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
  25.        
  26.         [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
  27.     }
  28.     else if (aspectRatio > 1) {
  29.         //horizontal image
  30.         CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio, backgroundView.frame.size.height - 2*contentInset);
  31.         CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
  32.         CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
  33.        
  34.         [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
  35.     }
  36.     else if (aspectRatio == 1) {
  37.         //square image
  38.         CGRect imageRect = CGRectMake(0, 0, backgroundView.frame.size.height - 2*contentInset, backgroundView.frame.size.height - 2*contentInset);
  39.         CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
  40.         CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);
  41.        
  42.         [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
  43.     }
  44.    
  45.     UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
  46.    
  47.     UIGraphicsEndImageContext();
  48.    
  49.     return finalImage;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement