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

- (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),由于该边缘,另一个边缘似乎应该伸出newRect。当我们使用newRect时,edge会大于UIGraphicsGetImageFromCurrentImageContext()的边缘。那是我模糊的“理解”。

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

最佳答案

我只是接受或添加了以前的评论,并试图更清楚地解释每个部分。您似乎已经掌握了基本思路,所以我希望这有助于巩固一切。

- (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();
    }

08-15 23:21