Tuesday, October 11, 2011

Dynamic Image Load to the UIImageView in iPhone

Here you can use this class as image loader, because in this code you can add default image (Until load our image it display default image).

this is how it call 


      MCDynamicImage *itemImage = [[MCDynamicImage alloc] initWithURL:itemImageUrl 
                                               withUseCache:self.mainCategoryCacheStatus withLoadingImage:loadingImage];
               
               

MCDynamicImage.h File



#import "Foundation/Foundation.h"


@interface MCDynamicImage : UIImageView {
        // MCDynamicImage class is subclass of UIImageView
   
    BOOL    resizeAfterLoad;
    CGRect    selfFrame;
}
@property(assign) BOOL resizeAfterLoad;

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage;
- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage enableResize:(BOOL)enable forFrame:(CGRect)framef;

@end

MCDynamicImage.m File



#import "MCDynamicImage.h"
#import "MCAppConfiguration.h"
#import "MCWebAccess.h"

@interface MCDynamicImage()

- (void)categoryImageCallback:(MCWebAccess *)webAccessAsync;
- (id)setDefaultImage:(UIImage *)loadingImage;
- (void)loadimageURL:(NSString *)url withUseCache:(int)useCache;
- (void)setActualImageFrame:(UIImage *)fullImage;

@end

@implementation MCDynamicImage

@synthesize resizeAfterLoad;

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage
{
    [self setDefaultImage:loadingImage];
    [self loadimageURL:url withUseCache:useCache];
    return self;
}

- (id)initWithURL:(NSString *)url withUseCache:(int)useCache withLoadingImage:(UIImage *)loadingImage enableResize:(BOOL)enable forFrame:(CGRect)framef
{
    self.resizeAfterLoad = enable;
    self.frame = framef;
    selfFrame = framef;
    return [self initWithURL:url withUseCache:useCache withLoadingImage:loadingImage];
}


    //Set default image to the reqest UIImageView 
- (id)setDefaultImage:(UIImage *)loadingImage
{
    return [self initWithImage:loadingImage];
}

    //Request the image, Asynchronously according to the 'cacheStatus'
- (void)loadimageURL:(NSString *)url withUseCache:(int)useCache
{
    MCWebAccess* webAccessAsync = [[MCWebAccess alloc]init];
    [webAccessAsync getDataAsync:url withMethod:@"GET" withData:nil withCacheStatus:useCache
                    withDelegate:self withSelector:@selector(categoryImageCallback:)];
   
    [webAccessAsync release];
}
    //Set response image to the request UIImageView
- (void)categoryImageCallback:(MCWebAccess *)webAccessAsync
{
    if(webAccessAsync.webAccessResult != nil && webAccessAsync.webAccessResponseCode >=200 && webAccessAsync.webAccessResponseCode < 300){
        UIImage *imageG = [[UIImage alloc] initWithData:webAccessAsync.webAccessResult];
       
       
       
        if (resizeAfterLoad) {
            [self setActualImageFrame:imageG];
        }
       
        self.image = imageG;
        [imageG release];
    }
}

- (void)setActualImageFrame:(UIImage *)fullImage
{
    CGRect parentFrame = selfFrame;
    CGSize imageSize = fullImage.size;
   
    double H = parentFrame.size.height;
    double W = parentFrame.size.width;
   
    double h = imageSize.height;
    double w = imageSize.width;
   
    if ((H >= h) && (W >= w)) {//image is smaller than frame
        self.frame = CGRectMake((W-w)/2, (H-h)/2, w, h);
    }else {
        double tempW = W;
        double tempH = W*h/w;
       
        if (tempH > H) {
            tempH = H;
            tempW = H*w/h;
            self.frame = CGRectMake((W-tempW)/2, 0, tempW, H);
        }else {
            self.frame = CGRectMake(0, (H-tempH)/2, W, tempH);
        }

    }

   
}

@end

No comments:

Post a Comment

Add your comment