Detailed explanation of common views and image processing examples in IOS

Time:2021-10-8

preface

As we all know, it is inevitable to encounter some image and view processing in development. These I summarize here are just some I encounter for next use. I won’t say much now. Let’s take a look at the detailed introduction.

Rotation of picture

Is an extension class of uiimage, which can be called directly using the object of uiimage

UIImage

#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>
 
@implementation UIImage (ImageRotate)
-(UIImage *)imageRotateIndegree:(float)degree{
 //1.image-》context
 size_t width = (size_t)(self.size.width *self.scale);
 size_t height = (size_t)(self.size.height*self.scale);
 
 size_ t bytesPerRow = width * 4;// Indicates bytes of picture data per line
 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), self.CGImage);
 //2 rotation
 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:self.scale orientation:self.imageOrientation];
 return rotateImage;
}
@end

Image clipping

It is still the extension class of uiimage. You can call it directly using the object of uiimage

UIImage


@implementation UIImage (ImageCut)
 
-(UIImage *)ImageCutSize:(CGRect)rect{
 CGImageRef subImageref = CGImageCreateWithImageInRect(self.CGImage, rect);
 CGRect smallRef = CGRectMake(0, 0, CGImageGetWidth(subImageref), CGImageGetHeight(subImageref));
 
 UIGraphicsBeginImageContext(smallRef.size);
 
 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(self.frame.size);
 [self.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imageNew;
}
@end

usage method

UIView

- (void)imageScreen{
 UIImage *imageNew = [self.view imageScreenShot];
 UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil); // If you want to save it directly in the album, you need to obtain the permission of the album
}

Picture scale processing

It is still an extension class of uiimage

UIImage


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

View add fillet

Here is the extension class of uiview, which is applicable to all views. You can set the adding location

UIView

@implementation UIView (LSCore)
 
/**
 Sets the absolute layout of partial fillets
 
 @Param corners need to be set as the corner uirectcorner topleft | uirectcorner topright of the fillet
 @Param radii the fillet size to be set cgsizemake (5.0, 5.0)
 */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:rounded.CGPath];
 self.layer.mask = shape;
}
 
 
/**
 Set the relative layout of partial fillets
 
 @Param corners need to be set as the corner uirectcorner topleft | uirectcorner topright of the fillet
 
 @Param radii the fillet size to be set cgsizemake (5.0, 5.0)
 @Param rect the fillet view to be set
 */
- (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii viewRect:(CGRect)rect{
 UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
 CAShapeLayer *shape = [[CAShapeLayer alloc] init];
 [shape setPath:rounded.CGPath];
 self.layer.mask = shape;
}
@end

Take 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(rect.size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 
 CGContextSetFillColorWithColor(context, [color CGColor]);
 CGContextFillRect(context, rect);
 
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return image;
}

Image adding system filter

UIImage


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

summary

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

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]