Skip to content
Browse files

Update resizing demo code

  • Loading branch information...
1 parent ea3f6a3 commit 272f06732df70b5f97f7d16221daadefff24794b @danielctull committed Jul 7, 2012
View
74 DCTImageCaccheDemo/AppDelegate.m
@@ -10,14 +10,26 @@
#import "ViewController.h"
#import <DCTImageCache/DCTImageCache.h>
+
+// setDelegateQueue was broken on iOS 5...
+BOOL CanSetDelegateQueue() {
+
+ Class collectionViewClass = NSClassFromString(@"UICollectionView");
+ Class tableViewClass = NSClassFromString(@"UITableView");
+
+ if (tableViewClass && !collectionViewClass) return NO;
+
+ return YES;
+}
+
@implementation AppDelegate {
__strong NSMutableDictionary *_datas;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_datas = [NSMutableDictionary new];
-
+
DCTImageCache *imageCache = [DCTImageCache defaultImageCache];
imageCache.imageFetcher = ^(NSString *key, CGSize size) {
[self fetchImageForKey:key size:size];
@@ -33,11 +45,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
- (void)fetchImageForKey:(NSString *)key size:(CGSize)size {
if (CGSizeEqualToSize(size, CGSizeZero)) {
- dispatch_async(dispatch_get_main_queue(), ^{
- NSURL *URL = [NSURL URLWithString:key];
- NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
- [NSURLConnection connectionWithRequest:request delegate:self];
- });
+ NSURL *URL = [NSURL URLWithString:key];
+ NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
+ NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
+
+ if (CanSetDelegateQueue())
+ [connection setDelegateQueue:[NSOperationQueue currentQueue]];
+
+ [connection start];
+
+ if (!CanSetDelegateQueue())
+ CFRunLoopRun();
+
return;
}
@@ -50,6 +69,7 @@ - (void)fetchImageForKey:(NSString *)key size:(CGSize)size {
#pragma mark - NSURLConnectionDownloadDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
+
NSURL *URL = connection.originalRequest.URL;
NSMutableData *imageData = [_datas objectForKey:URL];
@@ -67,33 +87,41 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage *image = [UIImage imageWithData:data];
DCTImageCache *imageCache = [DCTImageCache defaultImageCache];
[imageCache setImage:image forKey:[URL absoluteString] size:CGSizeZero];
+
+ if (!CanSetDelegateQueue())
+ CFRunLoopStop(CFRunLoopGetCurrent());
}
- (UIImage *)imageFromImage:(UIImage *)image toFitSize:(CGSize)size {
CGImageRef imageRef = image.CGImage;
- CGFloat height = image.size.height;
- CGFloat width = image.size.width;
-
- if (height < width) {
- NSInteger x = (NSInteger)(width-height)/2.0f;
- imageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake((CGFloat)x, 0.0f, height, height));
-
- } else if (height > width) {
- NSInteger y = (NSInteger)(height-width)/2.0f;
- imageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0.0f, y, width, width));
-
- } else {
- CGImageRetain(imageRef);
+ CGImageRetain(imageRef);
+ CGFloat imageHeight = image.size.height;
+ CGFloat imageWidth = image.size.width;
+ CGFloat imageRatio = imageHeight/imageWidth;
+
+ CGFloat height = size.height;
+ CGFloat width = size.width;
+ CGFloat ratio = height/width;
+
+ CGRect imageRect = CGRectMake(0.0f, 0.0f, width, height);
+
+ if (imageRatio < ratio) {
+ NSInteger newImageWidth = imageWidth * height/imageHeight;
+ NSInteger x = (NSInteger)(width-newImageWidth)/2.0f;
+ imageRect = CGRectMake((CGFloat)x, 0.0f, (CGFloat)newImageWidth, height);
+ } else if (imageRatio > ratio) {
+ NSInteger newImageHeight = imageHeight * width/imageWidth;
+ NSInteger y = (NSInteger)(height-newImageHeight)/2.0f;
+ imageRect = CGRectMake(0.0f, y, width, newImageHeight);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, colorSpace, kCGImageAlphaNoneSkipLast);
-
- CGRect imageRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
+ CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaNoneSkipLast);
+ CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
CGContextSetFillColor(context, CGColorGetComponents([UIColor whiteColor].CGColor));
- CGContextFillRect(context, imageRect);
+ CGContextFillRect(context, rect);
CGContextDrawImage(context, imageRect, imageRef);
View
10 DCTImageCaccheDemo/ViewController.m
@@ -19,9 +19,15 @@ - (void)viewDidLoad {
[super viewDidLoad];
DCTImageCache *imageCache = [DCTImageCache defaultImageCache];
+
+ NSString *URLString = @"http://apod.nasa.gov/apod/image/1207/saturntitan2_cassini_960.jpg";
+
+ CGFloat screenScale = [[UIScreen mainScreen] scale];
+ CGFloat width = screenScale * self.imageView.bounds.size.width;
+ CGFloat height = screenScale * self.imageView.bounds.size.height;
- [imageCache fetchImageForKey:@"http://apod.nasa.gov/apod/image/1207/saturntitan2_cassini_960.jpg"
- size:self.imageView.bounds.size
+ [imageCache fetchImageForKey:URLString
+ size:CGSizeMake(width, height)
handler:^(UIImage *image) {
dispatch_async(dispatch_get_main_queue(), ^{
View
3 DCTImageCaccheDemo/en.lproj/ViewController.xib
@@ -2,7 +2,7 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1280</int>
- <string key="IBDocument.SystemVersion">11E53</string>
+ <string key="IBDocument.SystemVersion">11E2705</string>
<string key="IBDocument.InterfaceBuilderVersion">2809</string>
<string key="IBDocument.AppKitVersion">1138.47</string>
<string key="IBDocument.HIToolboxVersion">569.00</string>
@@ -50,6 +50,7 @@
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="345550398"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MC43NQA</bytes>

0 comments on commit 272f067

Please sign in to comment.
Something went wrong with that request. Please try again.