簡體   English   中英

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

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

我需要實際的像素位置,而不是相對於UIImageView框架的位置,而是UIImage上的實際像素位置。

UIpangesture識別器提供者在UIimageView中的位置,因此沒有用。

我可以將x和y乘以比例,但UIImage比例始終為0。

我需要從UIImage裁剪圓形區域以使其模糊並將其完全放置在同一位置

流:

  1. 使用UIimage裁剪圓形區域:g CGImageCreateWithImageInRect

  2. 然后使用以下命令對圖像進行校正:[[UIBezierPath bezierPathWithRoundedRect:

  3. 使用CIGaussianBlur模糊圓角rect圖像

  4. 將圓形矩形模糊圖像放置在x,y位置

第一步,我需要用戶點擊的實際像素位置

這取決於圖像視圖內容模式。

對於比例填充模式,您只需將坐標乘以圖像與視圖的比例即可:

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

對於所有其他模式,您需要計算視圖內的實際圖像幀,然后使用不同的過程。

從評論中添加寬高比擬合模式:

為了獲得寬高比,您需要計算實際的圖像幀,該圖像幀可以比其中一個維度的圖像視圖幀小,並放置在中心:

    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

只是要注意,如果要轉移到方面填充,您需要做的就是在兩個if語句中交換<>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM