Date
Jul. 14th, 2024
 
2024年 6月 9日

Post: iOS : 模拟微信红包照片

iOS : 模拟微信红包照片

Published 12:01 Jan 28, 2016.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

上一篇博客用 HTML5 + CSS3 实现了一个简单的微信红包照片的效果,那当然少不了街机 iPhone 中的实现。

iOS 实现这个效果常用的有三种方式:

  • CoreImage 绘图

  • CALayer

  • UIVisualEffectView

其实都比较简单,这里介绍一下第一种。首先创建一个 UIImageView 供展示图片。

UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgView];

接下来,准备一张图片,例如:

image

通过 CoreImage 制作模糊效果:

UIImage *img = [UIImage imageNamed:@"img"];
CIImage *inputImg = [CIImage imageWithCGImage:img.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey, inputImg,@"inputRadius", @20, nil];

CIImage *blurImg = [filter outputImage];
CGImageRef blurRef = [context createCGImage:blurImg fromRect:CGRectMake(0, 0, img.size.width, img.size.height)];//[blurImg extent]];

UIImage *blur = [UIImage imageWithCGImage:blurRef];

得到了模糊图片(UIImage *blur)后,还需要制作一个圆形图片。我们把这两张图拼在一起。

UIGraphicsBeginImageContext(img.size);
[blur drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx, 160, 280, 50, 50, 0, M_PI);
CGContextClip(ctx);
[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

最后,展示图片:

imgView.image = final;

看看效果:

image2

Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF