Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Diogenes Buarque
7,804 PointsFace switch
Hey guys, i'm trying make a face switch app but i get a little problem with share view. The problem that is the face origin don't scale to fit in the face destination.
My code:
-(void) rebuildImage {
//Get original image size
float originalImageWidth = self.originalImage.size.width;
float originalImageHeight = self.originalImage.size.height;
//Get extracted image size
float extractedImageWidth = self.extractionImage.size.width;
float extractedImageHeight = self.extractionImage.size.height;
//Calculate ratio
float xRatio = originalImageWidth / extractedImageWidth;
float yRatio = originalImageHeight / extractedImageHeight;
//Begin context
UIGraphicsBeginImageContext(CGSizeMake(originalImageWidth, originalImageHeight));
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw original image
[self.originalImage drawInRect:CGRectMake(0, 0, originalImageWidth, originalImageHeight) blendMode: kCGBlendModeNormal alpha: 1.0];
//Get current face
FaceView *theFace = [self.faceViews objectAtIndex: self.faceIndex];
//Rotate image
UIImage *rotatedImage = [self.originalImage imageRotatedByDegrees: theFace.extractionAngle Anchor:CGPointMake(theFace.extractionAnchor.x * xRatio, theFace.extractionAnchor.y * yRatio)];
//Face image
UIImage *faceImage = [rotatedImage imageAtRect: CGRectMake(theFace.extractionFrame.origin.x * xRatio, theFace.extractionFrame.origin.y * yRatio, theFace.extractionFrame.size.width * xRatio, theFace.extractionFrame.size.height * yRatio)];
//Draw faces
for (int i=0; i<[self.faceViews count];i++) {
//Get current and next
FaceView *face = [self.faceViews objectAtIndex: i];
//Mask image
UIImage *maskImage = [UIImage imageNamed: [NSString stringWithFormat: @"Mask%i.png", face.maskIndex]];
//Calculate masked face
UIImage *maskedFace = [self getMaskedImage: faceImage MaskImage: maskImage];
//Apply saturation, brightness, flip
maskedFace = [Utilities staturateImage: maskedFace saturation: face.saturation];
maskedFace = [Utilities brightnessImage: maskedFace brightness: face.brightness];
//ALPHA BLENDING BUG HACK
NSData *tempData = UIImagePNGRepresentation(maskedFace);
maskedFace = [UIImage imageWithData:tempData];
//Apply flip
maskedFace = [UIImage imageWithCGImage: maskedFace.CGImage scale:1.0 orientation: face.flipped ? UIImageOrientationUpMirrored : UIImageOrientationUp];
//Save context
CGContextSaveGState(context);
//Apply translation, rotation, scale
CGContextTranslateCTM(context, (face.currentCenter.x - face.extractionOffsetX) * xRatio, (face.currentCenter.y - face.extractionOffsetY) * yRatio);
CGContextRotateCTM(context, face.currentRotation);
CGContextScaleCTM(context, face.currentScale, face.currentScale);
//Draw image
[maskedFace drawAtPoint:CGPointMake((-face.extractionFrame.size.width/2) * xRatio, (-face.extractionFrame.size.height/2) * yRatio) blendMode:kCGBlendModeNormal alpha: 1.0];
//Restore context
CGContextRestoreGState(context);
}
//Get result image
self.finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Display final image
self.backgroundImageView.image = self.finalImage;
}
The face should be this way:
Any idea?
Regards!
1 Answer
Amit Bijlani
Treehouse Guest TeacherYour self.backgroundImageView should probably be set for Aspect Fit.
Diogenes Buarque
7,804 PointsDiogenes Buarque
7,804 Pointswhere ?
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherThere are several ways you can set it. If the
backgroundImageViewis an IBOutlet to an image view in your storyboard then in the attributes inspector there is aModedrop down. Else you can set it using code:self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;Diogenes Buarque
7,804 PointsDiogenes Buarque
7,804 PointsActually, is really an IBOutlet and it already is setted to Aspect Fit, i don't use storyboard board but i use xib for views.
I tried add the code, but nothing new happen.
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherIt was just a hunch. However, looking at your code and image seems like the code is doing exactly what you would expect it to do, except that the baby's face is smaller than the woman's face. There's nothing in your code that is instructing the woman's face to be smaller as per the size of the baby's face.