views:

31

answers:

0

Ok i have searched all over and I can't not seem to find the answer I need. :-(

Here is what I am doing and what I need to happen. I have a UIImageView that I am able to transfrom using UIPanGestureRecognizer, UIRotationGestureRecognizer, and UIPinchGestureRecognizer all that works great. The problem comes when it is time to save those transfomations to my PhotoAlbum. The results are not what i am expecting. So far here is the code that i am using (al-be-it very incomplete)

-(IBAction)saveface:(id)sender {

 static int kMaxResolution = 640;

 CGImageRef imgRef = face.image.CGImage;
 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);

 CGRect bounds = CGRectMake(0, 0, width, height);
 if (width > kMaxResolution || height > kMaxResolution) {
      CGFloat ratio = width/height;
      if (ratio > 1) {
           bounds.size.width = kMaxResolution;
           bounds.size.height = bounds.size.width / ratio;
      } else {
           bounds.size.height = kMaxResolution;
           bounds.size.width = bounds.size.height * ratio;
      }
 }


 UIGraphicsBeginImageContext(bounds.size);
 CGContextRef context = UIGraphicsGetCurrentContext();

 CGContextScaleCTM(context, 1, -1);
 CGContextTranslateCTM(context, 0, -height);


 NSLog(@"SAVE  TX:%f TY:%f A:%f B:%f C:%f D:%f", face.transform.tx, face.transform.ty, face.transform.a,face.transform.b,face.transform.c,face.transform.d);
 CGFloat x = face.transform.tx;
 CGFloat y = face.transform.ty;

 CGContextTranslateCTM (context, x,-y);

 CGContextConcatCTM(context, face.transform);

 CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
 UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();


 UIImageWriteToSavedPhotosAlbum(imageCopy, self, nil, nil);

}

From this method I am able to save the image however the coordinates are way off. I can see any scaling i did that works ok. I see my rotation but it seems to be backwards, but the paning is WAY off the coords are all wrong and for the life of me i can't figure out what is wrong. I admit I am new to objective C and i have never used Quartz 2D before and my understanding of the transform matrixs is limited. I do understand that my transformations are not on the image data it self as trying to save the image out right with out applying this context to it does nothing. So please can anyone set me straight on this?