SoFunction
Updated on 2025-04-03

Detailed explanation of common view and image processing examples in iOS

Preface

As we all know, it is inevitable to encounter some images and views processing in development. The ones I have summarized here are just some of the ones I have encountered for next time I use them. I won’t say much below, let’s take a look at the detailed introduction together.

Image rotation

It is an extension class of UIImage, just use the UIImage object to call it directly

UIImage

#import <QuartzCore/>
#import <Accelerate/>
 
@implementation UIImage (ImageRotate)
-(UIImage *)imageRotateIndegree:(float)degree{
 //-》context
 size_t width = (size_t)( *);
 size_t height = (size_t)(*);
 
 size_t bytesPerRow = width * 4;//Indicate the bytes of the image data in each row CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;//alpha
 //Configure context parameters CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo);
 if (!bmContext) {
 return nil;
 }
 CGContextDrawImage(bmContext, CGRectMake(0, 0, width, height), );
 //2Spin UInt8 *data = (UInt8*)CGBitmapContextGetData(bmContext);
 vImage_Buffer src = {data,height,width,bytesPerRow};
 vImage_Buffer dest = {data,height,width,bytesPerRow};
 Pixel_8888 bgColor = {0,0,0,0};
 vImageRotate_ARGB8888(&src, &dest, NULL, degree, bgColor, kvImageBackgroundColorFill);
 //3context-》UIImage
 CGImageRef rotateImageref = CGBitmapContextCreateImage(bmContext);
 UIImage *rotateImage = [UIImage imageWithCGImage:rotateImageref scale: orientation:];
 return rotateImage;
}
@end

Cropping of pictures

It is still an extension class of UIImage, just use the UIImage object to call it directly

UIImage

@implementation UIImage (ImageCut)
 
-(UIImage *)ImageCutSize:(CGRect)rect{
 CGImageRef subImageref = CGImageCreateWithImageInRect(, rect);
 CGRect smallRef = CGRectMake(0, 0, CGImageGetWidth(subImageref), CGImageGetHeight(subImageref));
 
 UIGraphicsBeginImageContext();
 
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextDrawImage(context, smallRef, subImageref);
 UIImage *image = [UIImage imageWithCGImage:subImageref];
 
 UIGraphicsEndImageContext();
 return image;
}
@end

Get screenshots

Screenshot is an extension class of UIView

UIView

@implementation UIView (imageScreenShot)
- (UIImage *)imageScreenShot
{
 UIGraphicsBeginImageContext();
 [ renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imageNew;
}
@end

How to use

UIView

- (void)imageScreen{
 UIImage *imageNew = [ imageScreenShot];
 UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil); //Save it directly in the album and obtain the album permissions} 

Image proportional processing

It's still the extension class of UIImage

UIImage

@implementation UIImage (imageScaleSize)
 
- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize{
 UIGraphicsBeginImageContext(CGSizeMake( * scaleSize,  * scaleSize));
 [image drawInRect:CGRectMake(0, 0,  * scaleSize,  * scaleSize)];
 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
  return scaledImage;
}
@end

Add rounded corners to view

Here is the extension class of UIView, which is suitable for all Views, and you can set the location of adding it.

UIView

@implementation UIView (LSCore)
 
/**
  Set some rounded corners Absolute layout
 
  @param corners Need to be set to rounded corners UIRectCornerTopLeft|UIRectCornerTopRight
  @param radii The rounded corner size that needs to be set CGSizeMake(5.0, 5.0)
  */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect: byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:];
  = shape;
}
 
 
/**
  Set some rounded corners Relative layout
 
  @param corners Need to be set to rounded corners UIRectCornerTopLeft|UIRectCornerTopRight
 
  @param radii The rounded corner size that needs to be set CGSizeMake(5.0, 5.0)
  @param rect The rect of the rounded corner view that needs to be set
  */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii viewRect:(CGRect)rect{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect: byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:];
  = shape;
}
@end

UIImageView as an example

UIImage

[image addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(20.0, 20.0)];

Convert color to picture

UIImage

-(UIImage *)ImageForColor:(UIColor *)color{
 CGRect rect = CGRectMake(0.0f, 0.0f, 10, 10);
 UIGraphicsBeginImageContext();
 CGContextRef context = UIGraphicsGetCurrentContext();
 
 CGContextSetFillColorWithColor(context, [color CGColor]);
 CGContextFillRect(context, rect);
 
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return image;
}

Add system filters to pictures

UIImage

-(UIImage *)blurryImage:(UIImage *)image
   withBlurLevel:(CGFloat)blur {
 CIContext *context = [CIContext contextWithOptions:nil];
 CIImage *inputImage = [CIImage imageWithCGImage:];
 CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"
         keysAndValues:kCIInputImageKey, inputImage,
      @"inputRadius", @(blur),
      nil];
 
 CIImage *outputImage = ;
 CGImageRef outImage = [context createCGImage:outputImage
          fromRect:[outputImage extent]];
 
 return [UIImage imageWithCGImage:outImage];
}

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.