How to save an image using UIImageWriteToSavedPhotosAlbum after converting it

Ok, I searched everything and I can not find the answer that I need. :-(

This is what I do and what I need. I have a UIImageView that I can transform with UIPanGestureRecognizer, UIRotationGestureRecognizer and UIPinchGestureRecognizer all that works fine. The problem arises when it is time to save these translations in my photo album. The results are not what I expect. So far, here is the code I'm using (al-be-it is 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 can save the image, but the coordinates go away. I see any scaling I did, which works fine. I see my rotation, but it seems that it is the opposite, and panning is the WAY.

, , . , Objective-C, Quartz 2D, . , , , , . , , - ?

+3
2

, .

, - x y. , :

 CGFloat y = face.transform.tx;
 CGFloat x = face.transform.ty;

face . , . , self.face, , .

.

. , , , . , .

+2

, UIView, , . .

- :

CGSize vscaledSize = myOriginalImage.size;

//add in the scaled bits of the view
//scaling
CGFloat wratio = vscaledSize.width/self.pictureView.frame.size.width;
CGFloat vhightScaled = self.pictureView.frame.size.height * wratio;
vscaledSize.height = vhightScaled;

CGFloat hratio = vscaledSize.height/self.pictureView.frame.size.height;

//create context
UIGraphicsBeginImageContext(myOriginalImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context); //1 original context

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, vscaledSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

//original image
CGContextDrawImage(context, CGRectMake(0,0,vscaledSize.width,vscaledSize.height), myOriginalImage.CGImage);

CGContextRestoreGState(context);//1  restore to original;

//scale context to match view size
CGContextSaveGState(context); //1 pre-scaled size
CGContextScaleCTM(context, wratio, hratio);

//render 
[self.pictureView.layer renderInContext:context];

CGContextRestoreGState(context);//1  restore to pre-scaled size;

UIImage *exportImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

, , UIView.layer.

,

0

Source: https://habr.com/ru/post/1755550/


All Articles