如何缓存图像并正确缩放? [英] How to cache image and scale it correctly?

查看:121
本文介绍了如何缓存图像并正确缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用SDWebimage并以这种方式调用它,不确定是否正在缓存或如何检查?另外,如何在不拉伸的情况下进行相应缩放.图片最初是512 x 512,我想按比例缩小.我看到这篇文章不确定我是否应该使用这种方法? 在下载内部图像后调整UICollectionView单元的大小

I am currently using SDWebimage and calling it like so, not sure if it is caching or how I can check it? Also, how do I scale it accordingly without it looking stretched. The picture is originally 512 x 512 and I want to scale it down. I saw this post was not sure if I should be using this method? Resize UICollectionView cells after image inside has been downloaded

    //setting up each cell
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                     cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CollectionGridCell *myCell = [collectionView
                                        dequeueReusableCellWithReuseIdentifier:@"GridCell"
                                        forIndexPath:indexPath];


        long row = [indexPath row];

           NSURL *baseUrl = [NSURL URLWithString:@"http://example.com/"];

    NSString *imageItemName = [homeImages objectAtIndex:row];
    NSURL *url = [NSURL URLWithString:imageItemName relativeToURL:baseUrl];

   // NSURL *url = /* prepare a url... see note below */
    [myCell.homeImage setImageWithURL:url
                     placeholderImage:[UIImage imageNamed:@"menuButton.png"]
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                                // inspect cacheType here to make sure it's cached as you want it
                               myCell.homeImage.image = [self resizeImage:image newSize:CGSizeMake(75,75)];

                            }];

    return myCell;

}


- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();

    return newImage;
}

推荐答案

SDWebImage文档表示它会自动缓存.至于缩放,有很多的净代码像这样大约.诀窍是将其与SDWebImage集成.幸运的是,它提供了一个完成块:

SDWebImage docs says it caches automatically. As for scaling, there's plenty of net code like this around. The trick is integrating it with SDWebImage. Fortunately, it provides a completion block:

NSURL *url = /* prepare a url... see note below */
[myCell.homeImage setImageWithURL:url
               placeholderImage:[UIImage imageNamed:@"menuButton.png"]
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    // inspect cacheType here to make sure it's cached as you want it
    myCell.homeImage.image = [self scaleImage:image toSize:CGSizeMake(75,75)];
}];

一个简单的标尺看起来像这样(未经测试):

A simple scale would look something like this (not tested):

- (UIImage *)scale:(UIImage *)image toSize:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

偶然地,我注意到您使用通用字符串操作来构建URL.您最好对字符串使用专门的方法,如下所示:

Incidentally, I notice that you use generic string manipulation to build the URL. You'd be better off using the specialized methods on string as follows:

// this can be defined outside cellForRowAtIndexPath
NSURL *baseUrl = [NSURL urlWithString:@"http://example.com/"];

NSString *imageItemName = [homeImages objectAtIndex:row];
NSURL *url = [NSURL URLWithString:imageItemName relativeToURL:baseURL];

这篇关于如何缓存图像并正确缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆