views:

589

answers:

1

hi i'm new in iphone programming and i just want some help. Could you identify potential memory link in my code ? thanks a lot.

ImageCache.h

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>

@interface ImageCache : NSObject {

}


+(NSString *)getMD5FromString:(NSString *)chaine;
+(UIImage *)getImageFromCacheWithURL:(NSString *)url;
+(void)setImage:(UIImage *)image toCacheWithURL:(NSString *)url;
+(BOOL)isFileAtURL:(NSString *)url; 

@end

ImageCache.m

#import "ImageCache.h"
#define TMP NSTemporaryDirectory()


@implementation ImageCache



+(NSString *)getMD5FromString:(NSString *)chaine
{
    const char *cStr = [chaine UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result );
    return [NSString stringWithFormat:
      @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
      result[0], result[1], result[2], result[3], 
      result[4], result[5], result[6], result[7],
      result[8], result[9], result[10], result[11],
      result[12], result[13], result[14], result[15]
      ];  
}

+(UIImage *)getImageFromCacheWithURL:(NSString *)url
{
    NSString *urlMD5 = [self getMD5FromString:url];
    return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@%@",TMP,urlMD5]]; 
}

+(void)setImage:(UIImage *)image toCacheWithURL:(NSString *)url
{
    NSString *urlMD5 = [self getMD5FromString:url];
    NSString *uniquePath = [NSString stringWithFormat:@"%@%@",TMP,urlMD5];
    //[urlMD5 release];
    if([url rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
     [UIImagePNGRepresentation(image) writeToFile: uniquePath atomically: YES];
    else 
    if([url rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound || [url rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound)
     [UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];

}

+(BOOL)isFileAtURL:(NSString *)url 
{
    NSString *urlMD5 = [self getMD5FromString:url];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    //[urlMD5 release];
    return [filemanager fileExistsAtPath:[NSString stringWithFormat:@"%@%@",TMP,urlMD5]];
}


@end

ImageCache2ViewController.h

#import <UIKit/UIKit.h>
#import "UIImageViewCached.h"

@interface ImageCache2ViewController : UIViewController {

    IBOutlet UIImageViewCached *imageView;
    IBOutlet UITextField *txtField;
}

@property (nonatomic, retain) IBOutlet UIImageViewCached *imageView;
@property (nonatomic,retain) IBOutlet UITextField *txtField;

-(IBAction)reset;
-(IBAction)trace;
-(IBAction)load;
-(IBAction)hideKeyboard;

@end

ImageCache2ViewController.m

#import "ImageCache2ViewController.h"
#import "ImageCache.h"
#define TMP NSTemporaryDirectory()

@implementation ImageCache2ViewController
@synthesize imageView;
@synthesize txtField;

-(IBAction)load
{
    [self.imageView setImageFromURL:self.txtField.text];

}

-(IBAction)hideKeyboard
{
    [txtField resignFirstResponder];
}
-(IBAction)trace
{
    NSArray *temp = [[NSFileManager defaultManager] directoryContentsAtPath:TMP];
    for(int i=0; i< [temp count]; i++)
     NSLog([temp objectAtIndex:i]);
}

-(IBAction)reset
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *temp = [fileManager directoryContentsAtPath:TMP];
    if([temp count] == 0)
     NSLog(@"Pas d'élément");
    else
    {
     for(int i=0; i< [temp count]; i++)
     {
      NSString *str = [temp objectAtIndex:i];
      NSLog(str);
      //if([str hasPrefix:@"perso"])
      if(![fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@", TMP,str] error:nil])
       NSLog(@"Erreur");
     }
    }

}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

NSURLConnectionInfo.h

#import <Foundation/Foundation.h>


@interface NSURLConnectionInfo : NSURLConnection {
    NSString *url;
}

@property (nonatomic,retain) NSString *url;

-(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate forURL:(NSString *)urlArg;
-(void)dealloc;
@end

NSURLConnectionInfo.m

#import "NSURLConnectionInfo.h"


@implementation NSURLConnectionInfo

@synthesize url;

-(id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate forURL:(NSString *)urlArg
{
    if(self = [super initWithRequest:request delegate:delegate])
    {
     self.url =[[NSString alloc] initWithString:urlArg];
    }

    return self;
}
-(void)dealloc
{
    self.url = nil;
    [super dealloc];
}

@end

UIImageViewCached.h

#import <Foundation/Foundation.h>
#import "NSURLConnectionInfo.h"


@interface UIImageViewCached : UIImageView {

    NSMutableData *data;
    NSURLConnectionInfo *connection;

}

@property (nonatomic, retain) NSMutableData *data;
@property (nonatomic, retain) NSURLConnectionInfo *connection;


-(void)dealloc;
-(void)setImageFromURL:(NSString *)urlArg;


@end

UIImageViewCached.m

#import "UIImageViewCached.h"
#import "ImageCache.h"

@implementation UIImageViewCached


@synthesize data;
@synthesize connection;


-(void)setImageFromURL:(NSString *)urlArg
{
    //Le fichier que l'on doit récupérer n'existe pas dans le cache
    if(![ImageCache isFileAtURL:urlArg])
    {
     NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlArg] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
     connection = [[NSURLConnectionInfo alloc] initWithRequest:request delegate:self forURL:urlArg];

     if(connection)
      data = [[NSMutableData alloc] retain];
    }
    //Le fichier que l'on doit récupérer existe dans le cache
    else 
    {
     UIImage *img = [ImageCache getImageFromCacheWithURL:urlArg];
     self.image = img;
     [self setNeedsDisplay];
    }

}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    self.connection = nil;

    NSURLConnectionInfo * conn = (NSURLConnectionInfo *)theConnection;
    NSString *url = [NSString stringWithString:conn.url];

    UIImage *img = [UIImage imageWithData:data];
    [ImageCache setImage:img toCacheWithURL:url];
    self.image = img;
    self.contentMode = UIViewContentModeScaleAspectFit;
    self.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
    [self setNeedsLayout];

    self.data = nil;
}


-(void)dealloc {
    if(self.connection != nil)
    {
     [connection cancel];
     self.connection = nil;
    }
    self.data = nil;
    [super dealloc];
}

@end
+14  A: 

I'm sorry, that's not how this game works.

http://developer.apple.com/DOCUMENTATION/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html

Use those tools to find the leaks yourself.

Ben Hughes