Cropping images for faster cache.

This commit is contained in:
Samuel Clay 2014-02-11 13:23:45 -08:00
parent 810d28c650
commit 61a7f8e190
6 changed files with 150 additions and 32 deletions

View file

@ -13,6 +13,7 @@
#import "ABTableViewCell.h"
#import "UIView+TKCategory.h"
#import "UIImageView+AFNetworking.h"
#import "UIImage+Resize.h"
#import "Utilities.h"
#import "MCSwipeTableViewCell.h"
#import "TMCache.h"
@ -144,7 +145,13 @@ static UIFont *indicatorFont = nil;
storyImageView.image = cachedImage;
[storyImageView setContentMode:UIViewContentModeScaleAspectFill];
[storyImageView setClipsToBounds:YES];
[self addSubview:storyImageView];
CGFloat alpha = 1.0f;
if (cell.highlighted || cell.selected) {
alpha = 0.6f;
} else if (cell.isRead) {
alpha = 0.3f;
}
[storyImageView.image drawInRect:imageFrame blendMode:Nil alpha:alpha];
rect.size.width -= r.size.height;
} else {
NSLog(@"Fetching image: %@", cell.storyTitle);
@ -154,27 +161,32 @@ static UIFont *indicatorFont = nil;
[storyImageView setImageWithURLRequest:request
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (image.size.height < 50 || image.size.width < 50) {
[[TMCache sharedCache] setObject:[NSNull null]
forKey:cell.storyImageUrl];
return;
}
[[TMCache sharedCache] setObject:image
forKey:cell.storyImageUrl
block:
^(TMCache *cache, NSString *key, id object) {
if (cell.inDashboard) {
[appDelegate.dashboardViewController.storiesModule
showStoryImage:key];
} else {
[appDelegate.feedDetailViewController
showStoryImage:key];
}
}];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if (image.size.height < 50 || image.size.width < 50) {
[[TMCache sharedCache] setObject:[NSNull null]
forKey:cell.storyImageUrl];
return;
}
CGSize maxImageSize = CGSizeMake(300, 300);
image = [image imageByScalingAndCroppingForSize:maxImageSize];
[[TMCache sharedCache] setObject:image
forKey:cell.storyImageUrl
block:^(TMCache *cache, NSString *key, id object)
{
if (cell.inDashboard) {
[appDelegate.dashboardViewController.storiesModule
showStoryImage:key];
} else {
[appDelegate.feedDetailViewController
showStoryImage:key];
}
}];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
[[TMCache sharedCache] setObject:[NSNull null]
forKey:cell.storyImageUrl];
}];
}
}

View file

@ -335,17 +335,19 @@
}
- (void)showStoryImage:(NSString *)imageUrl {
for (FeedDetailTableCell *cell in [self.storyTitlesTable visibleCells]) {
if ([cell isKindOfClass:[NBLoadingCell class]]) return;
if ([cell.storyImageUrl isEqualToString:imageUrl]) {
NSIndexPath *indexPath = [self.storyTitlesTable indexPathForCell:cell];
NSLog(@"Reloading cell: %@ (%ld)", cell.storyTitle, (long)indexPath.row);
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
for (FeedDetailTableCell *cell in [self.storyTitlesTable visibleCells]) {
if (![cell isKindOfClass:[FeedDetailTableCell class]]) return;
if ([cell.storyImageUrl isEqualToString:imageUrl]) {
NSIndexPath *indexPath = [self.storyTitlesTable indexPathForCell:cell];
NSLog(@"Reloading cell: %@ (%ld)", cell.storyTitle, (long)indexPath.row);
[self.storyTitlesTable beginUpdates];
[self.storyTitlesTable reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
});
withRowAnimation:UITableViewRowAnimationNone];
[self.storyTitlesTable endUpdates];
}
}
}
});
}
#pragma mark -

View file

@ -555,7 +555,7 @@
content.emailItem = emailItem;
OSKSMSContentItem *smsItem = [[OSKSMSContentItem alloc] init];
smsItem.body = [NSString stringWithFormat:@"%@\n\n--\n\n%@\n\n", title, [url absoluteString]];;
smsItem.body = [NSString stringWithFormat:@"%@\n%@", title, [url absoluteString]];;
content.smsItem = smsItem;
OSKReadLaterContentItem *readLater = [[OSKReadLaterContentItem alloc] init];

View file

@ -176,6 +176,7 @@
FF1F13D318A9C2BE00FDA816 /* TMCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13CE18A9C2BE00FDA816 /* TMCache.m */; };
FF1F13D418A9C2BE00FDA816 /* TMDiskCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D018A9C2BE00FDA816 /* TMDiskCache.m */; };
FF1F13D518A9C2BE00FDA816 /* TMMemoryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D218A9C2BE00FDA816 /* TMMemoryCache.m */; };
FF1F13D818AAC97900FDA816 /* UIImage+Resize.m in Sources */ = {isa = PBXBuildFile; fileRef = FF1F13D718AAC97900FDA816 /* UIImage+Resize.m */; };
FF22FE4616E410A60046165A /* folder_collapse.png in Resources */ = {isa = PBXBuildFile; fileRef = FF22FE4416E410A60046165A /* folder_collapse.png */; };
FF22FE4716E410A60046165A /* folder_expand.png in Resources */ = {isa = PBXBuildFile; fileRef = FF22FE4516E410A60046165A /* folder_expand.png */; };
FF22FE4A16E413C30046165A /* disclosure_border@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FF22FE4816E413C30046165A /* disclosure_border@2x.png */; };
@ -838,6 +839,8 @@
FF1F13D018A9C2BE00FDA816 /* TMDiskCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMDiskCache.m; sourceTree = "<group>"; };
FF1F13D118A9C2BE00FDA816 /* TMMemoryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TMMemoryCache.h; sourceTree = "<group>"; };
FF1F13D218A9C2BE00FDA816 /* TMMemoryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TMMemoryCache.m; sourceTree = "<group>"; };
FF1F13D618AAC97900FDA816 /* UIImage+Resize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+Resize.h"; path = "Other Sources/UIImage+Resize.h"; sourceTree = "<group>"; };
FF1F13D718AAC97900FDA816 /* UIImage+Resize.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+Resize.m"; path = "Other Sources/UIImage+Resize.m"; sourceTree = "<group>"; };
FF22FE4416E410A60046165A /* folder_collapse.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = folder_collapse.png; sourceTree = "<group>"; };
FF22FE4516E410A60046165A /* folder_expand.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = folder_expand.png; sourceTree = "<group>"; };
FF22FE4816E413C30046165A /* disclosure_border@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "disclosure_border@2x.png"; sourceTree = "<group>"; };
@ -1522,6 +1525,8 @@
43A4C3D615B00966008787B5 /* UIView+TKCategory.m */,
FF4151BE16DED9660013E84B /* UIBarButtonItem+Image.h */,
FF4151BF16DED9660013E84B /* UIBarButtonItem+Image.m */,
FF1F13D618AAC97900FDA816 /* UIImage+Resize.h */,
FF1F13D718AAC97900FDA816 /* UIImage+Resize.m */,
);
name = "Other Sources";
sourceTree = "<group>";
@ -3130,6 +3135,7 @@
FF793E1C13F1A9F700F282D2 /* ASIDataDecompressor.m in Sources */,
FF5EA47F143B691000B7563D /* AddSiteViewController.m in Sources */,
FF265EB5187B635A0080C332 /* OSKAccountChooserViewController.m in Sources */,
FF1F13D818AAC97900FDA816 /* UIImage+Resize.m in Sources */,
FFD887F01445F1E800385399 /* AddSiteAutocompleteCell.m in Sources */,
FFE5322F144C8AC300ACFDE0 /* Utilities.m in Sources */,
FF265E47187B635A0080C332 /* PocketAPILogin.m in Sources */,

View file

@ -0,0 +1,15 @@
//
// UIImage+Resize.h
// NewsBlur
//
// Created by Samuel Clay on 2/11/14.
// Copyright (c) 2014 NewsBlur. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Resize)
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end

View file

@ -0,0 +1,83 @@
//
// UIImage+Resize.m
// NewsBlur
//
// Created by Samuel Clay on 2/11/14.
// Copyright (c) 2014 NewsBlur. All rights reserved.
//
#import "UIImage+Resize.h"
@implementation UIImage (Resize)
#pragma mark -
#pragma mark Scale and crop image
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
{
scaleFactor = widthFactor; // scale to fit height
}
else
{
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
{
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
NSLog(@"could not scale image");
}
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
@end