欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-11 23:41:16
前言 众所周知在开发中不可避免的会遇到一些图片和视图的处理,我这里总结的这些只是我遇到的一些,以供下次使用查看。下面话不多说了,来一起看看详细的介绍吧。 图片的旋转...

前言

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

图片的旋转

是uiimage的扩展类,直接使用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;//表明每行图片数据字节
 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];
}

总结

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