【发布时间】: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