Created
September 5, 2010 18:29
-
-
Save jankeesvw/566221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ImageLoader.m | |
// liteapp | |
// | |
// Created by Jankees van Woezik on 31-8-10. | |
// Copyright 2010 Base 42. All rights reserved. | |
// | |
#import "ImageLoader.h" | |
@implementation ImageLoader | |
-(UIImageView *)loadAndResizeImage:(NSString *)url inWidth:(Float32)width inHeight:(Float32)height { | |
NSLog(@"[ImageLoader loadAndResizeImage start %@]",url); | |
Float32 screenHeight = height; | |
Float32 screenWidth = width; | |
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; | |
UIImage *myimage = [[UIImage alloc] initWithData:imageData]; | |
Float32 fx = screenWidth / myimage.size.width; | |
Float32 fy = screenHeight / myimage.size.height; | |
Float32 scale = fmaxf(fx,fy); | |
CGSize newSize = CGSizeMake(myimage.size.width*scale, myimage.size.height*scale); | |
UIGraphicsBeginImageContext(newSize); | |
[myimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; | |
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
UIImageView *imageview = [UIImageView alloc]; | |
[imageview initWithImage:newImage]; | |
[imageview setFrame:CGRectMake((screenWidth-newSize.width)/2, (screenHeight-newSize.height)/2, newSize.width, newSize.height)]; | |
NSLog(@"[ImageLoader loadAndResizeImage done %@]",url); | |
return imageview; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment