简体   繁体   English

获取UIImageView中UIImage上平移的像素位置

[英]Get the pixel position of pan On UIImage in a UIImageView

I need the actual pixcel position not the positoin with respect to the UIImageView frame, but the actual pixcel position on UIImage. 我需要实际的像素位置,而不是相对于UIImageView框架的位置,而是UIImage上的实际像素位置。

UIpangesture recognizer giver the location in UIimageView, so it is of no use. UIpangesture识别器提供者在UIimageView中的位置,因此没有用。

I can multiply the x and y with scale, but the UIImage scale is always 0. 我可以将x和y乘以比例,但UIImage比例始终为0。

I need to crop a circular area from UIImage make it blur and place it exactly at the same position 我需要从UIImage裁剪圆形区域以使其模糊并将其完全放置在同一位置

Flow: 流:

  1. Crop circular area from an UIimage usin:g CGImageCreateWithImageInRect 使用UIimage裁剪圆形区域:g CGImageCreateWithImageInRect

  2. Then roud rect the image using: [[UIBezierPath bezierPathWithRoundedRect: 然后使用以下命令对图像进行校正:[[UIBezierPath bezierPathWithRoundedRect:

  3. Blur the round rect image using CIGaussianBlur 使用CIGaussianBlur模糊圆角rect图像

  4. Place the round rect blurred image at the x,y position 将圆形矩形模糊图像放置在x,y位置

In the first step I need the actual pixel position where the user tapped 第一步,我需要用户点击的实际像素位置

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. 只是要注意,如果要转移到方面填充,您需要做的就是在两个if语句中交换<>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM