0

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;
}
Community
  • 1
  • 1
Lion789
  • 4,402
  • 12
  • 58
  • 96

1 Answers1

1

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

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];
danh
  • 62,181
  • 10
  • 95
  • 136
  • ok with the block that I noticed as well, I had an issue with the return statement, as it error-ed inside of it? And the completion block would I still need the scale method too? – Lion789 Apr 26 '14 at 22:16
  • Not sure what you mean by an error in return statement. The scale method is needed to scale the image (UIImageView can be setup to make the image appear smaller, you'd need to call something like the scale I suggest to actually reduce the memory usage). I'm running out for a few hours, but happy to answer questions when I get back. – danh Apr 26 '14 at 22:22
  • Good. Check this out regarding blurry. Hopefully it will help: http://stackoverflow.com/questions/6141298/how-to-scale-down-a-uiimage-and-make-it-crispy-sharp-at-the-same-time-instead – danh Apr 27 '14 at 00:49
  • This is kind of a question about caching and scaling. Maybe you could get the community to help with a question specifically about image quality. I'd suggest posting the original as well as the result that's blurry. – danh Apr 28 '14 at 02:52