Saturday, September 14, 2013

AsyncImage


AsyncImage.h

#import <UIKit/UIKit.h>

#import "ASIHTTPRequest.h"
#import "CustomScrollBar.h"
#import <QuartzCore/QuartzCore.h>


@protocol AsyncImageDelegate

@optional
- (void) reSetFrame:(UIView *)newView;
@end

@interface AsyncImage : UIView<CustomScrollBarDelegate,UIScrollViewDelegate>
{
    NSURLConnection* connection; 
NSMutableData* data; 
UIImageView *image;
    UIImageView *imageView;
UIActivityIndicatorView *scrollingWheel;
    NSString *imgName;
    NSString *strImgurl;
    
    CGRect fullFrame;
    NSString *entryId;

    ASIHTTPRequest *request;
    CustomScrollBar *scroller;
    
    float heightNew;
    float imgWidth, imgHeight;
    
    NSObject<AsyncImageDelegate> *delegate;
}
@property(nonatomic,retain) NSString *entryId;
@property(nonatomic,retain)UIImageView *image;
@property(nonatomic,retain) NSURLConnection* connection;
@property (nonatomic, assign) NSObject<AsyncImageDelegate> *delegate;

-(void)loadImageFromString:(NSString*)url dict:(NSDictionary *)dict;
-(id) initWithFrame:(CGRect)frame;
-(NSString *)applicationDocumentsDirectory;
-(void)cancelConnection;
- (void)loadImageWhileSetObject:(NSString*)url;
-(float)getHeight : (UIImage *)img;
-(UIImage *)resizeImage:(UIImage *)image;
-(void)loadImageForVisibleCell:(NSString*)url dict:(NSDictionary *)dict;
@end

AsyncImage.m

#import "AsyncImage.h"
#import "FishbowlAppDelegate.h"

@implementation AsyncImage
FishbowlAppDelegate *appDelegate;

@synthesize entryId;
@synthesize image;
@synthesize connection;

@synthesize delegate;


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        
       imageView = [[UIImageView alloc] init];
        
        appDelegate = [FishbowlAppDelegate sharedAppDelegate];

scroller = [[CustomScrollBar alloc] initWithFrame:frame];
        //         scroller = [[CustomScrollBar alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
scroller.maximumZoomScale = 4.0;
scroller.minimumZoomScale = 1.0;
scroller.clipsToBounds = YES;
// a page is the width of the scroll view
//scroller.backgroundColor = [UIColor redColor];
scroller.scrollEnabled = YES;
scroller.multipleTouchEnabled = YES;
scroller.userInteractionEnabled = YES;
scroller.showsHorizontalScrollIndicator = YES;
scroller.showsVerticalScrollIndicator = YES;
scroller.scrollsToTop = NO;
scroller.delegate = self;
scroller.del = self;
scroller.contentSize = frame.size;

  scrollingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
float x = self.bounds.size.width/2;
float y = self.bounds.size.height/2;
scrollingWheel.center = CGPointMake(x, y);
scrollingWheel.hidesWhenStopped = YES;
       // [scroller addSubview:scrollingWheel];
       // self.clipsToBounds = YES;
        
[self addSubview:scrollingWheel];
        imageView.center = CGPointMake(x, y);
      //  [scroller addSubview:imageView];
        [self addSubview:scroller];
self.clipsToBounds = YES;
        
       // [scroller addSubview:imageView];
        }
    return self;
}
-(void)loadImageFromString:(NSString*)url dict:(NSDictionary *)dict;
{
    scrollingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    float x = self.bounds.size.width/2;
    float y = self.bounds.size.height/2;
    scrollingWheel.center = CGPointMake(x, y);
    scrollingWheel.hidesWhenStopped = YES;
    [self addSubview:scrollingWheel];

  [scrollingWheel startAnimating];
if (connection!=nil) {
        [connection cancel];
[connection release];
        
connection = nil;
        
}
    
    if (data!=nil) {
[data release];
data = nil;
}
if (image != nil) {
[image removeFromSuperview];
image = nil;
}

    if(dict == nil)
    {
        imgName =[[[url componentsSeparatedByString:@"/"] lastObject]retain];
    }
    else
    {
         imgName =[NSString stringWithFormat:@"%@_%@",[[[url componentsSeparatedByString:@"/"] lastObject]retain],[dict objectForKey:@"FacebookId"]];
    
    }
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];
    
    [imgName retain];
    
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if ([fileManager fileExistsAtPath:imagePath]==NO)
    {
        image.frame = self.bounds;
        image.layer.cornerRadius =5.0f;
    
        NSURLRequest* urequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:urequest delegate:self];
    }
    else
    {
        UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
         [UIImage imageWithContentsOfFile:imagePath];
        
     //   image = [[[UIImageView alloc] initWithImage:img] autorelease];
        image =[[UIImageView alloc] initWithImage:img];
        image.contentMode = UIViewContentModeScaleAspectFit;
        image.frame = self.bounds;
         image.layer.cornerRadius =5.0f;
[self addSubview:image];
    [scrollingWheel stopAnimating];
    }
}

-(void)loadImageForVisibleCell:(NSString*)url dict:(NSDictionary *)dict;
{
    scrollingWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    float x = self.bounds.size.width/2;
    float y = self.bounds.size.height/2;
    scrollingWheel.center = CGPointMake(x, y);
    scrollingWheel.hidesWhenStopped = YES;
    [self addSubview:scrollingWheel];
    
  [scrollingWheel startAnimating];
if (connection!=nil) {
        [connection cancel];
[connection release];
        
connection = nil;
}
    
    if (data!=nil) {
[data release];
data = nil;
}
if (image != nil) {
[image removeFromSuperview];
image = nil;
}
    
    if(dict == nil)
    {
        imgName =[[[url componentsSeparatedByString:@"/"] lastObject]retain];
    }
    else
    {
        imgName =[NSString stringWithFormat:@"%@_%@",[[[url componentsSeparatedByString:@"/"] lastObject]retain],[dict objectForKey:@"FacebookId"]];
        
    }
    NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];
    
    [imgName retain];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:imagePath]==NO)
    {
        image.frame = self.bounds;
        image.layer.cornerRadius =5.0f;
        
        NSURLRequest* urequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:urequest delegate:self];
    }
    else
    {
        UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
        [UIImage imageWithContentsOfFile:imagePath];
        
        //   image = [[[UIImageView alloc] initWithImage:img] autorelease];
        image =[[UIImageView alloc] initWithImage:img];
        image.contentMode = UIViewContentModeScaleAspectFit;
        image.frame = self.bounds;
        image.layer.cornerRadius =5.0f;
[self addSubview:image];
    [scrollingWheel stopAnimating];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [data release]; 
data=nil;
    [scrollingWheel stopAnimating];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
data = [[NSMutableData data] retain];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)dataObj {
[data appendData:dataObj];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)theConnection 
{
[connection release];
connection=nil;

     NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imgName];
    
    [data writeToFile:imagePath atomically:YES];
    
image = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
  //  image =[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
image.contentMode = UIViewContentModeScaleToFill;
    [image setClipsToBounds:YES];
    
  image.frame = self.bounds;
[self addSubview:image];
    
[data release]; 
data=nil;
[scrollingWheel stopAnimating];
    [imgName release];
}
-(NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
-(void)cancelConnection{
    
    if (connection !=nil) {
        [connection cancel];
        connection=nil;
    }
    if(data!=nil){
        [data release]; 
        data=nil;
    }
    
[scrollingWheel stopAnimating];
}
-(void)asyncImageSet:(UIImage *)img
{
    [scrollingWheel startAnimating];
if (connection!=nil) {
[connection release];
connection = nil;
}
if (data!=nil) {
[data release];
data = nil;
}
if (image != nil) {
[image removeFromSuperview];
image = nil;
}
    
    image = [[[UIImageView alloc] initWithImage:img] autorelease];
image.contentMode = UIViewContentModeScaleToFill;
    [image setClipsToBounds:YES];
    
    image.frame = self.bounds;
    
    [self addSubview:image];
    [scrollingWheel stopAnimating];
}
#pragma mark -
#pragma mark scrollView Delegate Methods
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return image;
}
#pragma mark -
#pragma mark Touch Delegate Methods
- (void)touchesBeganScroll:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesMovedScroll:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEndedScroll:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 1) {
        
        [scroller setZoomScale:1.0];
        
        [self setViewAnimation:CGRectMake(0,0, self.bounds.size.width, self.bounds.size.height)];
    }
}
-(void) setViewAnimation:(CGRect)frame {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
    image.frame = self.frame;
    scroller.contentSize = self.frame.size;
    [UIView commitAnimations];
}

- (void)dealloc {
 //   [imgName release];
[scrollingWheel release];
    [super dealloc];
}
@end

No comments:

Post a Comment