SoFunction
Updated on 2025-04-04

iOS development implements classification of UIImageView

This article shares the classification code for iOS implementation of UIImageView for your reference. The specific content is as follows

1.Objective-C version

.h file

#import <Foundation/>
#import <UIKit/>
#import <QuartzCore/>
 
/**
  * This category adds some useful methods to UIImageView
  */
@interface UIImageView (WLKit)
 
/**
  * Create a UIImageView
  *
  * @param image UIImageView image
  * @param rect UIImageView coordinates
  *
  * @return Return a UIImageView
  */
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
                   frame:(CGRect)rect;
 
/**
  * Create a UIImageView
  *
  * @param image UIImageView image
  * @param size UIImageView size
  * @param center UIImageView's center
  *
  * @return Return a UIImageView
  */
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
                    size:(CGSize)size
                   center:(CGPoint)center;
 
/**
  * Create a UIImageView
  *
  * @param image UIImageView image
  * @param center UIImageView's center
  *
  * @return Returns the created UIImageView
  */
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image
                   center:(CGPoint)center;
 
/**
 * Create an UIImageView with an image and use it as a template with the given color
 *
 * @param image   UIImageView image
 * @param tintColor UIImageView tint color
 *
 * @return Returns the created UIImageView
 */
+ (instancetype _Nonnull)imageViewWithImageAsTemplate:(UIImage *_Nonnull)image
                      tintColor:(UIColor *_Nonnull)tintColor;
 
/**
 * Create a drop shadow effect
 *
 * @param color  Shadow's color
 * @param radius Shadow's radius
 * @param offset Shadow's offset
 * @param opacity Shadow's opacity
 */
- (void)setImageShadowColor:(UIColor *_Nonnull)color
           radius:(CGFloat)radius
           offset:(CGSize)offset
          opacity:(CGFloat)opacity;
 
/**
 * Mask the current UIImageView with an UIImage
 *
 * @param image The mask UIImage
 */
- (void)setMaskImage:(UIImage *_Nonnull)image;
 
@end

.m file

#import "UIImageView+"
 
@implementation UIImageView (WLKit)
 
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image frame:(CGRect)rect
{
  UIImageView *_image = [[UIImageView alloc] init];
  [_image setFrame:rect];
  [_image setImage:image];
  return _image;
}
 
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image size:(CGSize)size center:(CGPoint)center
{
  UIImageView *_image = [[UIImageView alloc] init];
  [_image setFrame:CGRectMake(0, 0, , )];
  [_image setImage:image];
  [_image setCenter:center];
  return _image;
}
 
+ (instancetype _Nonnull)imageViewWithImage:(UIImage *_Nonnull)image center:(CGPoint)center
{
  UIImageView *_image = [[UIImageView alloc] init];
  [_image setFrame:CGRectMake(0, 0, , )];
  [_image setImage:image];
  [_image setCenter:center];
  return _image;
}
 
+ (instancetype _Nonnull)imageViewWithImageAsTemplate:(UIImage *_Nonnull)image tintColor:(UIColor *_Nonnull)tintColor
{
  UIImageView *_image = [[UIImageView alloc] init];
  image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  [_image setImage:image];
  [_image setTintColor:tintColor];
  return _image;
}
 
- (void)setImageShadowColor:(UIColor *_Nonnull)color radius:(CGFloat)radius offset:(CGSize)offset opacity:(CGFloat)opacity
{
   = ;
   = radius;
   = offset;
   = opacity;
   = NO;
}
 
- (void)setMaskImage:(UIImage *_Nonnull)image
{
  CALayer *mask = [CALayer layer];
   = (id)[image CGImage];
   = CGRectMake(0, 0, , );
   = mask;
   = YES;
}
 
- (void)setAlpha:(CGFloat)alpha
{
  if ([ isKindOfClass:[UITableView class]]) {
    if ( == 836913) {
      if (alpha == 0 &&  == UIViewAutoresizingFlexibleLeftMargin) {
        if ( < 10 &&  > ) {
          UIScrollView *sc = (UIScrollView*);
          if ( < ) {
            [super setAlpha:0.5];
            return;
          }
        }
      }
    }
    
    if ( == 836914) {
      if (alpha == 0 &&  == UIViewAutoresizingFlexibleTopMargin) {
        if ( < 10 &&  < ) {
          UIScrollView *sc = (UIScrollView*);
          if ( < ) {
            return;
          }
        }
      }
    }
  }
  
  [super setAlpha:alpha];
}
@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.