【问题标题】:coordinate computation of the image thumbnail图像缩略图的坐标计算
【发布时间】:2014-01-25 11:31:10
【问题描述】:

这是一个代码 sn-p,用于创建缩略图大小的图像(从原始大图像)并将其适当地放置在 tableviewcell 的顶部。当我在研究代码时,我卡在了通过设置其横坐标和纵坐标为缩略图指定位置的部分。在 -(void)setThumbDataFromImage:(UIImage *)image 方法中,他们正在设置项目缩略图的尺寸和坐标——

   -(void)setThumbnailDataFromImage:(UIImage *)image{
CGSize origImageSize= [image size];

      //    the rectange of the thumbnail
         CGRect newRect= CGRectMake(0, 0, 40, 40);

   //    figure out a scaling ratio to make sure we maintain the same aspect ratio
         float ratio= MAX(newRect.size.width/origImageSize.width,   newRect.size.height/origImageSize.height);

  //       Create a transparent bitmap context with a scaling factor equal to that of the screen
     UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

    //    create a path that is a rounded rectangle
     UIBezierPath *path= [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];
    //    make all the subsequent drawing to clip to this rounded rectangle
       [path addClip];

    //    center the image in the thumbnail rectangle
      CGRect projectRect;
     projectRect.size.width=ratio * origImageSize.width;
     projectRect.size.height= ratio * origImageSize.height;
     projectRect.origin.x= (newRect.size.width- projectRect.size.width)/2;
     projectRect.origin.y= (newRect.size.height- projectRect.size.height)/2;

    //    draw the image on it
      [image drawInRect:projectRect];

     //    get the image from the image context, keep it as our thumbnail
      UIImage *smallImage= UIGraphicsGetImageFromCurrentImageContext();
       [self setThumbnail:smallImage];

     //    get the PNG representation of the image and set it as our archivable data
         NSData *data= UIImagePNGRepresentation(smallImage);
         [self setThumbnailData:data];

      //    Cleanup image context resources, we're done
       UIGraphicsEndImageContext();
    }

我得到了宽度和高度计算,其中我们将 origImageSize 与缩放因子/比率相乘。 但随后我们使用以下内容给缩略图一个位置——

     projectRect.origin.x= (newRect.size.width- projectRect.size.width)/2;
     projectRect.origin.y= (newRect.size.height- projectRect.size.height)/2;

这是我无法理解的。我无法绕过它。 :? 这是定心过程的一部分。我的意思是,我们在这里使用数学关系来定位缩略图还是一些随机计算,即可能是任何东西..我是否错过了这两行代码背后的一些基本原理??

【问题讨论】:

    标签: objective-c cocoa-touch cocoa core-graphics


    【解决方案1】:

    这两行代码是用于使某些东西居中的标准代码,尽管它们并不是以最通用的方式编写的。你通常想使用:

     projectRect.origin.x = newRect.origin.x + newRect.size.width / 2.0 - projectRect.size.width / 2.0;
     projectRect.origin.y = newRect.origin.y + newRect.size.height / 2.0 - projectRect.size.height / 2.0;
    

    在您的情况下,作者知道原点是 0,0,因此他们省略了每行中的第一项。

    由于要将矩形居中在另一个矩形中,因此您希望两个轴的中心对齐,因此您取容器宽度的一半(外部矩形的中心)并减去内部矩形宽度的一半(这需要你到内部矩形的左侧),当它正确居中时,它会告诉你内部矩形的左侧应该在哪里(例如:它的 x 原点)。

    【讨论】:

    • 所以它用于将 newRect(外部矩形)和 projRect(内部矩形)的中心对齐在一起。 projRect.origin.x= newRect.origin.x + newRect.size.center.x - projRect.size.width/2;和projRect.origin.y= newRect.origin.y + newRect.size.center.y - projRect.size.height/2。但是如果 projRect 是矩形的(考虑 origImage 是矩形的)怎么办?然后我们将内部正方形和外部矩形的中心对齐,但矩形的原点不会在 (0,0)。因此,如果在运行时显示在 tableviewcell 上,矩形不会在边缘被切断。
    • 在矩形场景中,考虑宽度与正方形相同,但高度大于正方形(newRect 为 40X40)。所以缩略图会向上移动单元格并且不合适。所以这不是一个问题。
    • 如果内部内容大于外部内容,它将被此代码裁剪。我假设作者对“缩略图”的定义是“最大 40x40 的图像”。
    • 是的,我把 40X40 newRect 误认为是内部矩形。我的意思是它是外部矩形(作为容器)。 projRect 是内部矩形(它是作为图像占位符的包含项目),假设为 40X50 尺寸(考虑 origImage 本身为矩形),将其上部裁剪掉。
    • 所以创建缩略图背后的过程是首先创建一个容器(一般是正方形)并在坐标空间中给它一个位置。然后通过保持原始图像的纵横比创建一个缩略图框架(内部矩形/包含项),然后在容器的坐标系中给它一个位置(通过对齐中心)。缩略图占位符准备好后,将其粘贴进去。如果我遗漏了什么,请告诉我..
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 2014-11-05
    • 2012-11-02
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多