gpt4 book ai didi

ios - 根据 UIScrollView 内的视口(viewport)裁剪 UIImage

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:15 24 4
gpt4 key购买 nike

我在 UIScrollView 中有一个 UIImageView(用 Aspect Fill 初始化)。用户应使用它来调整 UIImage 的大小和位置到他喜欢的位置,按下按钮,然后他在屏幕上看到的内容将被发送到服务器,仅在原始图像坐标中。换句话说,生成的图像会比屏幕上的图像大。

到目前为止,这是我的代码的重要部分:

// select the original 
CGFloat imageRatio = self.imageView.image.size.width / self.imageView.image.size.height;
CGFloat viewRatio = self.imageView.frame.size.width / self.imageView.frame.size.height;
CGFloat scale;

// get the scale factor of the image being "aspect-filled"
if(imageRatio < viewRatio) {
scale = self.imageView.image.size.width / self.imageView.frame.size.width;
} else {
scale = self.imageView.image.size.height / self.imageView.frame.size.height;
}

// get the cropping origin in the image's original coordinates
CGFloat originX = self.imageScrollView.contentOffset.x * scale;
CGFloat originY = self.imageScrollView.contentOffset.y * scale;
CGPoint origin = CGPointMake(originX, originY);

// get the cropping size in the image's original coordinates
CGFloat width = self.imageView.image.size.width / self.imageScrollView.zoomScale;
CGFloat height = self.imageView.image.size.height / self.imageScrollView.zoomScale;
CGSize size = CGSizeMake(width, height);

// create the new image from the original one
CGRect imageRect = CGRectMake(origin.x, origin.y, size.width, size.height);
struct CGImage *croppedCGImage = CGImageCreateWithImageInRect(self.imageView.image.CGImage, imageRect);

它看起来已经非常接近我想要的。原点计算至少看起来是正确的。但是,生成的图像仍然存在一些差异,我猜这是由于可能图像的纵横比不同所致。可能我需要以某种方式将 scale 添加到我的 size 方程中,但我真的无法理解如何

希望有人能帮我把这最后两米...

[编辑]

根据第一条评论,我尝试了以下方法。但不知何故,我的 targetFrame 始终与 scrollFrame 相同。对我来说是不是太晚了才能做正确的事??

CGFloat zoom = self.imageScrollView.zoomScale;
// the full image resolution
CGRect fullImageFrame = CGRectMake(0, 0,
self.imageView.image.size.width,
self.imageView.image.size.height);
UIView *fullImageView = [[[UIView alloc] initWithFrame:fullImageFrame] autorelease];
// the theoretical size of the image on the screen
CGRect previewImageFrame = CGRectMake(0, 0,
self.imageView.frame.size.width * zoom,
self.imageView.frame.size.height * zoom);
UIView *previewImageView = [[[UIView alloc] initWithFrame:previewImageFrame] autorelease];
// the excerpt of previewImageFrame really visible
CGRect scrollFrame = CGRectMake(self.imageScrollView.contentOffset.x,
self.imageScrollView.contentOffset.y,
self.imageScrollView.frame.size.width,
self.imageScrollView.frame.size.height);
// that excerpt transferred to the full resolution image's coordinates
CGRect targetFrame = [fullImageView convertRect:scrollFrame fromView:previewImageView];

最佳答案

您可能希望探索:

(CGRect)convertRect:(CGRect)rect toView:(UIView *)view

此方法和其他类似方法允许您获取 View 并根据父 View 中的位置调整其坐标系,而不是最初打算或定位的父 View 。

关于ios - 根据 UIScrollView 内的视口(viewport)裁剪 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com