IOS loads image code asynchronously by clicking the button

From , 2 Years ago, written in Objective-C, viewed 204 times.
URL https://pastebin.vip/view/54ee290e
  1. @interface UIButton (AsyncImage)
  2.  
  3. //size by point
  4. - (void)setImageFromURL:(NSString *)urlString adjustToSize:(CGSize)size completion:(void (^)(void))completion logo:(UIImage *)logoImage;
  5.  
  6. @end
  7.  
  8.  
  9. @implementation UIButton (AsyncImage)
  10.  
  11. - (void)setImageFromURL:(NSString *)urlString adjustToSize:(CGSize)size completion:(void (^)(void))completion logo:(UIImage *)logoImage
  12. {
  13.     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  14.         UIImage *image = nil;
  15.         NSURL *url = [NSURL URLWithString:urlString];
  16.         NSData *data = [NSData dataWithContentsOfURL:url];
  17.         image = [UIImage imageWithData:data];
  18.          
  19.         if (image) {
  20.             if (!CGSizeEqualToSize(size, CGSizeZero)) {
  21.                 image = [UIImage imageWithCGImage:image.CGImage scale:[self scaleImage:image adjustToSize:size] orientation:image.imageOrientation];
  22.             }
  23.             if (logoImage) {
  24.                 image = [self addLogoImage:logoImage toImage:image];
  25.             }
  26.              
  27.             dispatch_async(dispatch_get_main_queue(), ^{
  28.                 [self setImage:image forState:UIControlStateNormal];
  29.                 completion();
  30.             });
  31.         }
  32.         else {
  33.             NSLog(@"async load error.");
  34.         }
  35.     });
  36. }
  37.  
  38. // 缩放图片以适应按钮大小
  39. - (CGFloat)scaleImage:(UIImage *)image adjustToSize:(CGSize)size
  40. {
  41.     CGFloat xScale = size.width / image.size.width;
  42.     CGFloat yScale = size.height / image.size.height;
  43.      
  44.     return 1.0 / MIN(xScale, yScale);
  45. }
  46.  
  47. - (UIImage *)addLogoImage:(UIImage *)logo toImage:(UIImage *)img
  48. {
  49.     //get image width and height
  50.     CGFloat scale = [UIScreen mainScreen].scale;
  51.     int w = scale * img.size.width;
  52.     int h = scale * img.size.height;
  53.     int logoWidth = logo.scale * logo.size.width;
  54.     int logoHeight = logo.scale * logo.size.height;
  55.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  56.      
  57.     //create a graphic context with CGBitmapContextCreate
  58.     CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  59.     CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  60.     CGContextDrawImage(context, CGRectMake(w - logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);
  61.     CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  62.     CGContextRelease(context);
  63.     CGColorSpaceRelease(colorSpace);
  64.      
  65.     return [UIImage imageWithCGImage:imageMasked scale:scale orientation:img.imageOrientation];
  66. }
  67.  
  68. @end
  69. //objectc/8372

Reply to "IOS loads image code asynchronously by clicking the button"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website