人脸识别,并生成包含所有人脸的合适UIImage
#import <QuartzCore/QuartzCore.h>
+(UIImage*)betterFaceWithImage:(UIImage*)image showSize:(CGSize)showSize
{
//低精度的探测:CIDetectorAccuracyLow已足够用。高精度的耗费时间。
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracyLow:CIDetectorAccuracy}];
NSArray* features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
CGSize imageSize = image.size;
CGRect fixedRect = CGRectMake(MAXFLOAT, MAXFLOAT, 0, 0);
CGFloat rightBorder = 0, bottomBorder = 0;
for (CIFaceFeature *f in features){
CGRect oneRect = f.bounds;
oneRect.origin.y = imageSize.height - oneRect.origin.y - oneRect.size.height;
fixedRect.origin.x = MIN(oneRect.origin.x, fixedRect.origin.x);
fixedRect.origin.y = MIN(oneRect.origin.y, fixedRect.origin.y);
rightBorder = MAX(oneRect.origin.x + oneRect.size.width, rightBorder);
bottomBorder = MAX(oneRect.origin.y + oneRect.size.height, bottomBorder);
}
fixedRect.size.width = rightBorder - fixedRect.origin.x;
fixedRect.size.height = bottomBorder - fixedRect.origin.y;
CGPoint fixedCenter = CGPointMake(fixedRect.origin.x + fixedRect.size.width / 2.0,
fixedRect.origin.y + fixedRect.size.height / 2.0);
CGPoint offset = CGPointZero;
CGSize finalSize = imageSize;
if (imageSize.width / imageSize.height > showSize.width / showSize.height) {
//move horizonal
finalSize.height = showSize.height;
finalSize.width = imageSize.width/imageSize.height * finalSize.height;
fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
offset.x = fixedCenter.x - showSize.width * 0.5;
if (offset.x < 0) {
offset.x = 0;
} else if (offset.x + showSize.width > finalSize.width) {
offset.x = finalSize.width - showSize.width;
}
offset.x = - offset.x;
} else {
//move vertical
finalSize.width = showSize.width;
finalSize.height = imageSize.height/imageSize.width * finalSize.width;
fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
//0.618 黄金分割
offset.y = fixedCenter.y - showSize.height * (1-0.618);
if (offset.y < 0) {
offset.y = 0;
} else if (offset.y + showSize.height > finalSize.height){
offset.y = finalSize.height = showSize.height;
}
offset.y = - offset.y;
}
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(offset.x,
offset.y,
finalSize.width,
finalSize.height);
layer.contents = (id)image.CGImage;
UIGraphicsBeginImageContextWithOptions(showSize, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
// 返回 一个基于当前图形上下文的图片
UIImage* betterFace = UIGraphicsGetImageFromCurrentImageContext();
// 移除栈顶 的基于当前位图的图形上下文
UIGraphicsEndImageContext();
return betterFace;
}
+(UIImage*)betterFaceWithImage:(UIImage*)image showSize:(CGSize)showSize
{
//低精度的探测:CIDetectorAccuracyLow已足够用。高精度的耗费时间。
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracyLow:CIDetectorAccuracy}];
NSArray* features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
CGSize imageSize = image.size;
CGRect fixedRect = CGRectMake(MAXFLOAT, MAXFLOAT, 0, 0);
CGFloat rightBorder = 0, bottomBorder = 0;
for (CIFaceFeature *f in features){
CGRect oneRect = f.bounds;
oneRect.origin.y = imageSize.height - oneRect.origin.y - oneRect.size.height;
fixedRect.origin.x = MIN(oneRect.origin.x, fixedRect.origin.x);
fixedRect.origin.y = MIN(oneRect.origin.y, fixedRect.origin.y);
rightBorder = MAX(oneRect.origin.x + oneRect.size.width, rightBorder);
bottomBorder = MAX(oneRect.origin.y + oneRect.size.height, bottomBorder);
}
fixedRect.size.width = rightBorder - fixedRect.origin.x;
fixedRect.size.height = bottomBorder - fixedRect.origin.y;
CGPoint fixedCenter = CGPointMake(fixedRect.origin.x + fixedRect.size.width / 2.0,
fixedRect.origin.y + fixedRect.size.height / 2.0);
CGPoint offset = CGPointZero;
CGSize finalSize = imageSize;
if (imageSize.width / imageSize.height > showSize.width / showSize.height) {
//move horizonal
finalSize.height = showSize.height;
finalSize.width = imageSize.width/imageSize.height * finalSize.height;
fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
offset.x = fixedCenter.x - showSize.width * 0.5;
if (offset.x < 0) {
offset.x = 0;
} else if (offset.x + showSize.width > finalSize.width) {
offset.x = finalSize.width - showSize.width;
}
offset.x = - offset.x;
} else {
//move vertical
finalSize.width = showSize.width;
finalSize.height = imageSize.height/imageSize.width * finalSize.width;
fixedCenter.x = finalSize.width / imageSize.width * fixedCenter.x;
fixedCenter.y = finalSize.width / imageSize.width * fixedCenter.y;
//0.618 黄金分割
offset.y = fixedCenter.y - showSize.height * (1-0.618);
if (offset.y < 0) {
offset.y = 0;
} else if (offset.y + showSize.height > finalSize.height){
offset.y = finalSize.height = showSize.height;
}
offset.y = - offset.y;
}
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(offset.x,
offset.y,
finalSize.width,
finalSize.height);
layer.contents = (id)image.CGImage;
UIGraphicsBeginImageContextWithOptions(showSize, NO, 0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
// 返回 一个基于当前图形上下文的图片
UIImage* betterFace = UIGraphicsGetImageFromCurrentImageContext();
// 移除栈顶 的基于当前位图的图形上下文
UIGraphicsEndImageContext();
return betterFace;
}