gpt4 book ai didi

iOS,图片缩略图,Big Nerd Ranch,第 19 章

转载 作者:行者123 更新时间:2023-11-29 10:23:13 25 4
gpt4 key购买 nike

我正在阅读 Big Nerd Ranch 的第 19 章,这是 iOS 教科书,无法理解接收大图像并从中创建缩略图的功能的几个部分。看看:

- (void)setThumbnailFromImage:(UIImage *)image
{
CGSize origImageSize = image.size;

// The rectangle 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 subsequent drawing 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.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;

[image drawInRect:projectRect];

// Get the image from the image context; keep it as our thumbnail
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
self.thumbnail = smallImage;

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

根据我的理解,我们得到两个比率的最大值,然后我们将原始图像的较小边缘等于 newRect 的边缘(在我们的例子中为 40),另一个当我们 UIGraphicsGetImageFromCurrentImageContext() 时,边缘似乎应该伸出 newRect,因为边缘会比 newRect 的边缘大。这是我模糊的“理解”。

谁能详细解释一下整个代码的作用,尤其是居中部分?如果您知道一些可能相关的教程,那就太好了。

最佳答案

我只是采纳或添加了之前的评论,并试图更清楚地解释每个部分。您似乎了解了基本概念,所以我希望这有助于巩固一切。

- (void)setThumbnailFromImage:(UIImage *)image
{
CGSize origImageSize = image.size;

//Create new rectangle of your desired size
CGRect newRect = CGRectMake(0, 0, 40, 40);

//Divide both the width and the height by the width and height of the original image to get the proper ratio.
//Take whichever one is greater so that the converted image isn't distorted through incorrect scaling.
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
// Basically everything within this builds the image
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

// Create a path that is a rounded rectangle -- essentially a frame for the new image
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect
cornerRadius:5.0];

// Applying path
[path addClip];

// Center the image in the thumbnail rectangle
CGRect projectRect;
// Scale the image with previously determined ratio
projectRect.size.width = ratio * origImageSize.width;
projectRect.size.height = ratio * origImageSize.height;
// I believe the anchor point of the new image is (0.5, 0.5), so here he is setting the position to be in the middle
// Half of the width and height added to whatever origin you have (in this case 0) will give the proper coordinates
projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;

// Add the scaled image
[image drawInRect:projectRect];

// Retrieving the image that has been created and saving it in memory
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
self.thumbnail = smallImage;

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

关于iOS,图片缩略图,Big Nerd Ranch,第 19 章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33772397/

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