首先确保在项目中已经安装了所需的第三方库。
点击【Podfile】,查看安装配置文件。
根据配置文件中的相关配置,安装第三方库。
1 platform :ios,‘12.0‘ 2 use_frameworks! 3 4 target ‘DemoApp‘ do 5 source ‘https://github.com/CocoaPods/Specs.git‘ 6 pod ‘FaceAware‘ 7 end
然后点击打开【DemoApp.xcworkspace】项目文件。
往项目中导入一张包含人像的图片:【Pic1】
在项目导航区,打开视图控制器的代码文件【ViewController.swift】
1 import UIKit 2 //在当前类文件中,引入已经安装的第三方类库 3 import FaceAware 4 5 class ViewController: UIViewController { 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 // Do any additional setup after loading the view,typically from a nib. 10 11 //通过图片的名称,从项目中加载该图片 12 let image = UIImage(named: "Pic1") 13 //初始化一个图像视图,并设置图像视图的显示区域 14 let imageView = UIImageView(frame: CGRect(x: 0,y: 0,width: 200,height: 200)) 15 //将图像视图放置在屏幕的中心位置 16 imageView.center = self.view.center 17 //设置图像视图需要显示的图片内容 18 imageView.image = image 19 //设置图像视图的层的圆角半径为100。 20 //由于图像视图的宽度和高度都是200,所以这里创建了一个圆形的图像视图。 21 imageView.layer.cornerRadius = 100 22 //设置图像视图的层的遮罩属性,裁剪在圆形之外的图像。 23 imageView.layer.masksToBounds = true 24 //图像视图的面部检测,聚焦图片人物面部位置,显示在图像视图焦点位置。 25 imageView.focusOnFaces = true 26 27 //将图像视图添加到当前视图控制器的根视图 28 self.view.addSubview(imageView) 29 } 30 31 override func didReceiveMemoryWarning() { 32 super.didReceiveMemoryWarning() 33 // dispose of any resources that can be recreated. 34 } 35 }