#import "ViewController.h" #import "KLFaceDetector.h" #define FACEIMGVIEW_TAG 100001 @interface ViewController () @property (nonatomic, strong) UIImageView * faceImgView; @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage * image = [self scaleImage:[UIImage imageNamed:@"IMG_2425.jpg"] toScale:0.1]; _imageView = [[UIImageView alloc]initWithImage:image]; [self.view addSubview:_imageView]; UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, _imageView.frame.size.height+200, _imageView.frame.size.width, 40); [btn setTitle:@"图片保存" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; btn1.frame = CGRectMake(0, _imageView.frame.size.height+260, _imageView.frame.size.width, 40); [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn1 setTitle:@"人脸识别" forState:UIControlStateNormal]; [btn1 addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1]; UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; btn2.frame = CGRectMake(0, _imageView.frame.size.height+310, _imageView.frame.size.width, 40); [btn2 setTitle:@"截图" forState:UIControlStateNormal]; [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(pressBtn2) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; UIImage * img = [UIImage imageNamed:@"Test.jpg"]; NSAssert((img), @"IMG not found"); CGSize imgSize = [img size]; CGSize imgViewSize = CGSizeMake(self.view.frame.size.height/2.0*imgSize.width/imgSize.height, self.view.frame.size.height/2.0); self.faceImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgViewSize.width, imgViewSize.height)]; [_faceImgView setImage:img]; [self.view addSubview:_faceImgView]; } -(void)pressBtn2{ CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; // 截图 UIGraphicsBeginImageContextWithOptions(CGSizeMake(screenWidth, screenHeight), YES, 0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); // 存入相册 UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); UIGraphicsEndImageContext(); } -(void)pressBtn{ UIImageView * imaView = (UIImageView*)[self.view viewWithTag:FACEIMGVIEW_TAG]; //图片保存到本地相册 UIImageWriteToSavedPhotosAlbum(imaView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } -(void)pressBtn1:(UIButton*)btn{ [[self.view viewWithTag:FACEIMGVIEW_TAG] removeFromSuperview]; CGSize imgSize = CGSizeMake(160, 120); // you can change this size to whatever you like [btn setTitle:@"Working..." forState:UIControlStateNormal]; [KLFaceDetector getImageWithFaceForImage:self.faceImgView.image withSize:imgSize shouldFast:YES completionHandler:^(UIImage * faceImg) { [btn setTitle:@"GetFace" forState:UIControlStateNormal]; UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn.frame)+15, CGRectGetMaxY(self.faceImgView.frame)+25, imgSize.width, imgSize.height)]; [imgView setBackgroundColor:[UIColor darkGrayColor]]; [imgView setTag:FACEIMGVIEW_TAG]; [imgView setImage:faceImg]; [self.view addSubview:imgView]; }]; } - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSString *message = @"呵呵"; if (!error) { message = @"成功保存到相册"; }else { } NSLog(@"message is %@",message); } //等比缩放 - (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize { //opaque 是否透明 scale 模糊度 UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width*scaleSize, image.size.height*scaleSize), YES,10); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } //自定义大小 - (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize { UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height)); [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)]; UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return reSizeImage; }