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

时间:2015-11-18 05:23:18

标签: ios objective-c

我正在阅读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();
}

根据我的理解,我们得到两个比率的MAX,然后我们将原始图像的较小边缘等于newRect的边缘(在我们的情况下为40),另一个边缘似乎应该超出newRect,因为当我们newRect时边缘会比UIGraphicsGetImageFromCurrentImageContext()的边缘大。这是我模糊的理解'。

有谁能请详细解释这整个代码的内容,尤其是中心部分?如果你知道一些可能相关的教程,它也会很棒。

1 个答案:

答案 0 :(得分:2)

我刚刚接受或添加了之前的评论,并试图更清楚地解释每个部分。你似乎得到了基本的想法,所以我希望这有助于巩固一切。

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