草庐IT

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

coder 2024-01-27 原文

我在 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/

有关ios - 根据 UIScrollView 内的视口(viewport)裁剪 UIImage的更多相关文章

  1. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  7. ruby - 如何根据长度将路径数组转换为嵌套数组或散列 - 2

    我需要根据字符串路径的长度将字符串路径数组转换为符号、哈希和数组的数组给定以下数组:array=["info","services","about/company","about/history/part1","about/history/part2"]我想生成以下输出,对不同级别进行分组,根据级别的结构混合使用符号和对象。产生以下输出:[:info,:services,about:[:company,history:[:part1,:part2]]]#altsyntax[:info,:services,{:about=>[:company,{:history=>[:part1,:pa

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. ruby - 如何在ruby中提取方括号内的内容 - 2

    我正在尝试提取方括号内的内容。到目前为止,我一直在使用它,它有效,但我想知道我是否可以直接在正则表达式中使用某些东西,而不是使用这个删除功能。a="Thisissuchagreatday[coolawesome]"a[/\[.*?\]/].delete('[]')#=>"coolawesome" 最佳答案 差不多。a="Thisissuchagreatday[coolawesome]"a[/\[(.*?)\]/,1]#=>"coolawesome"a[/(?"coolawesome"第一个依赖于提取组而不是完全匹配;第二个利用前瞻和

  10. ruby - 尝试比较两个文本文件,并根据信息创建第三个 - 2

    我有两个文本文件,master.txt和926.txt。如果926.txt中有一行不在master.txt中,我想写入一个新文件notinbook.txt。我写了我能想到的最好的东西,但考虑到我是一个糟糕的/新手程序员,它失败了。这是我的东西g=File.new("notinbook.txt","w")File.open("926.txt","r")do|f|while(line=f.gets)x=line.chompifFile.open("master.txt","w")do|h|endwhile(line=h.gets)ifline.chomp!=xputslineendende

随机推荐