2

I need to crop UIImage which is loaded in UIScrollview with some rect of Another UIView which is also in UIScrollView

Following is View Hierarchy

 --> View
     -->  UIScrollView
        --> viewBase (UIView)
             --> UIImageView -> (Zoomed & rotated )
             --> UIView (Target View)(Movable User can move anywhere in scrollview  to crop rect)

My Image is Rotated & Zoomed I need to get exact part of image in TargetView

I am drawing UIImage with rotation on context following is code

CGFloat angleCroppedImageRetreacted = atan2f(self.imgVPhoto.transform.b, self.imgVPhoto.transform.a);
angleCroppedImageRetreacted = angleCroppedImageRetreacted * (180 / M_PI);

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.imgVPhoto.image.size.width, self.imgVPhoto.image.size.height)];
rotatedViewBox.transform = CGAffineTransformMakeRotation(-angleCroppedImageRetreacted);
CGSize rotatedSize = rotatedViewBox.frame.size;

UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0f, rotatedSize.height / 2.0f);
CGContextRotateCTM(bitmap, -angleCroppedImageRetreacted);
CGContextScaleCTM(bitmap, 1.0f, -1.0f);



CGContextDrawImage(bitmap, CGRectMake(-self.imgVPhoto.image.size.width / 2.0f,
                                      -self.imgVPhoto.image.size.height / 2.0f,
                                      self.imgVPhoto.image.size.width,
                                      self.imgVPhoto.image.size.height),
                   self.imgVPhoto.image.CGImage);
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();




UIGraphicsEndImageContext();

And it works fine . I am getting Rotated UIImage same as i can see in Simulator

For converting Point of Target View to UIImage I use following code which is NOT WORKING

 CGPoint imageViewPoint = [self.viewBase convertPoint:self.targetImageview.center toView:self.imgVPhoto];

float percentX = imageViewPoint.x / self.imgVPhoto.frame.size.width;
float percentY = imageViewPoint.y / self.imgVPhoto.frame.size.height;

CGPoint imagePoint = CGPointMake(resultImage.size.width * percentX, resultImage.size.height * percentY);

rect.origin = imagePoint;

//rect.origin.x *= (self.imgVPhoto.image.size.width / self.imgVPhoto.frame.size.width);
//rect.origin.y *= (self.imgVPhoto.image.size.height / self.imgVPhoto.frame.size.height);


imageRef = CGImageCreateWithImageInRect([resultImage CGImage], rect);
img = [UIImage imageWithCGImage:imageRef scale:viewImage.scale orientation:viewImage.imageOrientation];

I think issue is we can't use Rect after Transform Apply

Please Help me to crop UIImage which is zoomed and rotated from rect on same Hierarchy

If you need more info pls ask

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • What is your relation between target view and image view? I am skeptical the rotation code will work if UIImage has orientation (try to take a portrait photo in runtime and put it through a few angles to confirm it really works). If you suspect that extracting frame from transformed view is an issue then use a standard approach where you save the current transform to a local variable, set the view transform to identity, save frame to a local variable set view transform back to saved value. – Matic Oblak Aug 24 '17 at 10:37
  • Unfortunately extracting coordinates from scroll view is a bit of a trial and error procedure. Then drawing everything to a graphics context may be troublesome again and everything escalates when image is rotated. But if your view is correctly drawn and you already see exactly what you need then maybe you should consider creating a view snapshot. Assume your image view was on another view which was the size and position of your target view. Then create a snapshot of that view at any time to get the image. You might want to scale it a bit to increase the output image size a bit... – Matic Oblak Aug 24 '17 at 10:42
  • @MaticOblak target view is a small UIView (Circle) which is a part to be **crop** means it is movable UIView which set by user to crop a specific part in image view – Prashant Tukadiya Aug 24 '17 at 10:42
  • @MaticOblak I did it !!!!! see my answer – Prashant Tukadiya Aug 24 '17 at 12:34

1 Answers1

0

I am answering my own question .

Thanks to Matic for giving a idea

I changed a logic

I have achieved same functionality what i looking for

CGPoint locationInImageView =  [self.viewBase convertPoint:self.targetImageview.center toView:self.view]; // received from touch
locationInImageView =  [self.view convertPoint:locationInImageView toView:self.imgVPhoto];

// I GOT LOCATION IN UIIMAGEVIEW OF TOUCH POINT

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);

[self.imgVPhoto.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *img1 = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// I GOT UIIMAGE FROM  CURRENT CONTEXT 

CGFloat width = self.targetImageview.frame.size.width * self.zoomScale ;
CGFloat height = self.targetImageview.frame.size.height * self.zoomScale ;

 //2 IS SCALE FACTOR 

CGFloat xPos = (locationInImageView.x  * 2)  - width / 2;
CGFloat yPos = (locationInImageView.y * 2) - height / 2;

CGRect rect1 = CGRectMake(xPos, yPos, width, height);

CGImageRef imageRef = CGImageCreateWithImageInRect([img1 CGImage], rect1);


// YAHHH YOU HAVE EXACT IMAGE 
UIImage *img = [UIImage imageWithCGImage:imageRef scale:img1.scale orientation:img1.imageOrientation];
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98