服务器之家

服务器之家 > 正文

iOS中常见的视图和图片处理示例详解

时间:2021-03-31 16:29     来源/作者:随风

前言

众所周知在开发中不可避免的会遇到一些图片和视图的处理,我这里总结的这些只是我遇到的一些,以供下次使用查看。下面话不多说了,来一起看看详细的介绍吧。

图片的旋转

是UIImage的扩展类,直接使用UIImage的对象调用即可

UIImage

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;//表明每行图片数据字节
 CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;//alpha
 //配置上下文参数
 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旋转
 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

图片的裁剪

依然是UIImage的扩展类,直接使用UIImage的对象调用即可

UIImage

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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

获取截屏

截屏是UIView的扩展类

UIView

?
1
2
3
4
5
6
7
8
9
10
@implementation UIView (imageScreenShot)
- (UIImage *)imageScreenShot
{
 UIGraphicsBeginImageContext(self.frame.size);
 [self.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return imageNew;
}
@end

使用方法

UIView

?
1
2
3
4
- (void)imageScreen{
 UIImage *imageNew = [self.view imageScreenShot];
 UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil); //直接保存在相册里,要获取相册权限
}

图片比例处理

依然是UIImage的扩展类

UIImage

?
1
2
3
4
5
6
7
8
9
10
@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添加圆角

这里是UIView的扩展类,适用于所有的View,可以设置添加的位置

UIView

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@implementation UIView (LSCore)
 
/**
 设置部分圆角 绝对布局
 
 @param corners 需要设置为圆角的角 UIRectCornerTopLeft|UIRectCornerTopRight
 @param radii 需要设置的圆角大小 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;
}
 
 
/**
 设置部分圆角 相对布局
 
 @param corners 需要设置为圆角的角 UIRectCornerTopLeft|UIRectCornerTopRight
 
 @param radii 需要设置的圆角大小 CGSizeMake(5.0, 5.0)
 @param rect 需要设置的圆角view的rect
 */
- (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

使用方法以UIImageView为例

UIImage

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

将颜色转为图片

UIImage

?
1
2
3
4
5
6
7
8
9
10
11
12
-(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;
}

图片添加系统滤镜

UIImage

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(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];
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://blog.duicode.com/2419.html

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部