Quote:
Originally Posted by yuri1984
Hi Peter,
Thank you for your reply...i tried what you said..i did'nt get error but its still same..  ....
|
The "Filename" string you are using to get the "UniquePath" must be unique itself. It must correspond to the filename of image you have cached. As you have it now all of the images are being cached under the filename "haberler3", so when you are retrieving the images it is always pointing to the same image. Give each image you want to cache a unique filename.
try this:
Code:
- (void) cacheImage: (NSString *) ImageURLString imageName:(NSString*)imageName
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
// Generate a unique path to a resource representing the image you want
NSString *filename = imageName;
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
// Check for file existence
if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
// The file doesn't exist, we should get a copy of it
// Fetch image
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
UIImage *image = [[UIImage alloc] initWithData: data];
// Do we want to round the corners?
image = [self roundCorners: image];
// Is it PNG or JPG/JPEG?
// Running the image representation function writes the data from the image to a file
if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImagePNGRepresentation(image) writeToFile: uniquePath atomically: YES];
}
else if(
[ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
[ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound
)
{
[UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];
}
}
}
- (UIImage *) getCachedImage: (NSString *) ImageURLString imageName:(NSString*)imageName
{
NSString *filename = imageName;
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
image = [UIImage imageWithContentsOfFile: uniquePath]; // this is the cached image
}
else
{
// get a new one
[self cacheImage: ImageURLString imageName:imageName];
image = [UIImage imageWithContentsOfFile: uniquePath];
}
return image;
}
and insert a different "imageName" for each image you want to cache.
I hope this helps.
Peter