1
votes

I need the actual pixcel position not the positoin with respect to the UIImageView frame, but the actual pixcel position on UIImage.

UIpangesture recognizer giver the location in UIimageView, so it is of no use.

I can multiply the x and y with scale, but the UIImage scale is always 0.

I need to crop a circular area from UIImage make it blur and place it exactly at the same position

Flow:

  1. Crop circular area from an UIimage usin:g CGImageCreateWithImageInRect

  2. Then roud rect the image using: [[UIBezierPath bezierPathWithRoundedRect:

  3. Blur the round rect image using CIGaussianBlur

  4. Place the round rect blurred image at the x,y position

In the first step I need the actual pixel position where the user tapped

1
I am facing same issue. I need UIImage Point from UIImageViewPoint Are you able to solve it ?Prashant Tukadiya
@MikeAlter it was quite a while ago. But I think I was able to get the "multiply the x and y with scale". I don't remember how. But scale and multiplication was how I was able to do it. Please do let me know, if you get the solution.nr5
Thanks for Replay Nil It not gonna help me , My problem is I have Scrollview + Zoom + Rotate . stackoverflow.com/q/45854149/4601900 This is question i have posted. But No answers !!Prashant Tukadiya

1 Answers

0
votes

It depends on the image view content mode.

For the scale to fill mode you need to simply multiply the coordinates with image to view ratio:

CGPoint pointOnImage = CGPointMake(pointOfTouch.x*(imageSize.width/frameSize.width), pointOfTouch.y*(imageSize.height/frameSize.height));

For all other modes you need to compute the actual image frame inside the view which have different procedures then.

Adding aspect fit mode from comments:

For aspect fit you need to compute the actual image frame which can be smaller then the image view frame in one of the dimensions and is placed in center:

    CGSize imageSize; // the original image size
    CGSize imageViewSize; // the image view size

    CGFloat imageRatio = imageSize.width/imageSize.height;
    CGFloat viewRatio = imageViewSize.width/imageViewSize.height;

    CGRect imageFrame = CGRectMake(.0f, .0f, imageViewSize.width, imageViewSize.height);

    if(imageRatio > viewRatio) {
        // image has room on top and bottom but fits perfectly on left and right
        CGSize displayedImageSize = CGSizeMake(imageViewSize.width, imageViewSize.width / imageRatio);
        imageFrame = CGRectMake(.0f, (imageViewSize.height-displayedImageSize.height)*.5f, displayedImageSize.width, displayedImageSize.height);
    }
    else if(imageRatio < viewRatio) {
        // image has room on left and right but fits perfectly on top and bottom
        CGSize displayedImageSize = CGSizeMake(imageViewSize.height * imageRatio, imageViewSize.height);
        imageFrame = CGRectMake((imageViewSize.width-displayedImageSize.width)*.5f, .0f, displayedImageSize.width, displayedImageSize.height);
    }

    // transform the coordinate
    CGPoint locationInImageView; // received from touch

    CGPoint locationOnImage = CGPointMake(locationInImageView.x, locationInImageView.y); // copy the original point
    locationOnImage = CGPointMake(locationOnImage.x - imageFrame.origin.x, locationOnImage.y - imageFrame.origin.y); // translate to fix the origin
    locationOnImage = CGPointMake(locationOnImage.x/imageFrame.size.width, locationOnImage.y/imageFrame.size.height); // transform to relative coordinates
    locationOnImage = CGPointMake(locationOnImage.x*imageSize.width, locationOnImage.y*imageSize.height); // scale to original image coordinates

Just a note if you want to ransfer to aspect fill all you need to do is swap < and > in both of the if statements.