热门标签 | HotTags
当前位置:  开发笔记 > 开发工具 > 正文

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

在日常ios开发中经常会遇到视图和图片的处理,下面这篇文章主要给大家总结介绍了关于iOS中常见的视图和图片处理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习和工作具有一定的参考学习价值,需要的朋友可以参考下。

前言

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

图片的旋转

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

UIImage

#import 
#import 
 
@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

@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

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

使用方法

UIView

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

图片比例处理

依然是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添加圆角

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

UIView

@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

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

将颜色转为图片

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;
}

图片添加系统滤镜

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];
}

总结

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


推荐阅读
  • 通过Web界面管理Linux日志的解决方案
    本指南介绍了一种利用rsyslog、MariaDB和LogAnalyzer搭建集中式日志管理平台的方法,使用户可以通过Web界面查看和分析Linux系统的日志记录。此方案不仅适用于服务器环境,还提供了详细的步骤来确保系统的稳定性和安全性。 ... [详细]
  • 本文介绍了Linux系统中的文件IO操作,包括文件描述符、基本文件操作函数以及目录操作。详细解释了各个函数的参数和返回值,并提供了代码示例。 ... [详细]
  • 简化报表生成:EasyReport工具的全面解析
    本文详细介绍了EasyReport,一个易于使用的开源Web报表工具。该工具支持Hadoop、HBase及多种关系型数据库,能够将SQL查询结果转换为HTML表格,并提供Excel导出、图表显示和表头冻结等功能。 ... [详细]
  • 在Fedora 31上部署PostgreSQL 12
    本文详细介绍如何在Fedora 31操作系统上安装和配置PostgreSQL 12数据库。包括环境准备、安装步骤、配置优化以及安全设置,确保数据库能够稳定运行并提供高效的性能。 ... [详细]
  • 本文探讨了如何在Classic ASP中实现与PHP的hash_hmac('SHA256', $message, pack('H*', $secret))函数等效的哈希生成方法。通过分析不同实现方式及其产生的差异,提供了一种使用Microsoft .NET Framework的解决方案。 ... [详细]
  • 本文详细介绍了如何在云服务器上配置Nginx、Tomcat、JDK和MySQL。涵盖从下载、安装到配置的完整步骤,帮助读者快速搭建Java Web开发环境。 ... [详细]
  • 本文将详细介绍如何在没有显示器的情况下,使用Raspberry Pi Imager为树莓派4B安装操作系统,并进行基本配置,包括设置SSH、WiFi连接以及更新软件源。 ... [详细]
  • CentOS 7.6环境下Prometheus与Grafana的集成部署指南
    本文旨在提供一套详细的步骤,指导读者如何在CentOS 7.6操作系统上成功安装和配置Prometheus 2.17.1及Grafana 6.7.2-1,实现高效的数据监控与可视化。 ... [详细]
  • 本文介绍了在MacOS上通过Homebrew安装Anaconda3,并配置环境变量以实现不同Python版本之间的快速切换。同时,提供了详细的步骤来创建和管理多个Python环境。 ... [详细]
  • 请看|间隔时间_Postgresql 主从复制 ... [详细]
  • NFS(Network File System)即网络文件系统,是一种分布式文件系统协议,主要用于Unix和类Unix系统之间的文件共享。本文详细介绍NFS的配置文件/etc/exports和相关服务配置,帮助读者理解如何在Linux环境中配置NFS客户端。 ... [详细]
  • 本文介绍如何在Linux系统中卸载预装的OpenJDK,安装指定版本的JDK 1.8,并配置防火墙以确保系统安全性和软件兼容性。 ... [详细]
  • 本文详细介绍了 Kubernetes 集群管理工具 kubectl 的基本使用方法,涵盖了一系列常用的命令及其应用场景,旨在帮助初学者快速掌握 kubectl 的基本操作。 ... [详细]
  • 利用SSH隧道实现外网对局域网机器的安全访问
    本文探讨了一种常见的网络配置问题及其解决方案,即如何在外网环境下安全地访问位于局域网内的计算机。特别介绍了使用SSH反向隧道技术来实现这一目标的具体步骤和注意事项。 ... [详细]
  • 在Linux系统上构建Web服务器的详细步骤
    本文详细介绍了如何在Linux系统上搭建Web服务器的过程,包括安装Apache、PHP和MySQL等关键组件,以及遇到的一些常见问题及其解决方案。 ... [详细]
author-avatar
beitianmolang
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有