在iOS中使用CIFaceFeature转换图像

我使用CIDetector和CIFaceFeature来检测前置摄像头的脸部。 还试图在头上戴帽子。 当头部笔直时帽子正好放好。 如果我倾斜我的头,帽子变小,远离头部。

用于添加帽子的代码,

self.hatImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:selectedImageName]]; self.hatImgView.contentMode = UIViewContentModeScaleAspectFit; [self.previewView addSubview:self.hatImgView]; 

检测面部和运动,

 - (void)detectedFaceController:(DetectFace *)controller features:(NSArray *)featuresArray forVideoBox:(CGRect)clap withPreviewBox:(CGRect)previewBox { for (CIFaceFeature *ff in featuresArray) { CGRect faceRect = [ff bounds]; //isMirrored because we are using front camera faceRect = [DetectFace convertFrame:faceRect previewBox:previewBox forVideoBox:clap isMirrored:YES]; float hat_width = self.hatImgView.image.size.width; float hat_height = self.hatImgView.image.size.height; int head_start_y = 330.0; //part of hat image is transparent int head_start_x = 60.0; float width = faceRect.size.width * (hat_width / (hat_width - head_start_x)); float height = width * hat_height/hat_width; int y = faceRect.origin.y - (height * head_start_y) / hat_height; int x = faceRect.origin.x - (head_start_x * width/hat_width); [UIView animateWithDuration:0.3 animations:^{ [self.hatImgView setTransform:CGAffineTransformRotate(CGAffineTransformIdentity, (DegreesToRadians(-ff.faceAngle)))]; self.hatImgView.frame = CGRectMake(x, y, width + 60.0f, height + 60.0f); }]; } } 

在此处输入图像描述 在此处输入图像描述

有谁知道如何用头部改造帽子? 所有的帮助表示赞赏!!