It looks like you're new here. If you want to get involved, click one of these buttons!
#import Foundation/Foundation.h
@interface Utilities : NSObject {
}
// Methods
- (void) cacheImage: (NSString *) ImageURLString;
- (UIImage *) getCachedImage: (NSString *) ImageURLString;
- (UIImage *) roundCorners: (UIImage*) img;
@end
#import \"Utilities.h\"
#define TMP NSTemporaryDirectory()
@implementation Utilities
- (void) cacheImage: (NSString *) ImageURLString
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
// Generate a unique path to a resource representing the image you want
NSString *filename = [[something unique, perhaps the image name]];
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
{
NSString *filename = [[something unique, perhaps the image name]];
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];
image = [UIImage imageWithContentsOfFile: uniquePath];
}
return image;
}
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0)
{
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM (context, ovalWidth, ovalHeight);
fw = CGRectGetWidth (rect) / ovalWidth;
fh = CGRectGetHeight (rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2);
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
CGContextClosePath(context);
CGContextRestoreGState(context);
}
- (UIImage *) roundCorners: (UIImage*) img
{
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
addRoundedRectToPath(context, rect, 5, 5);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
[img release];
return [UIImage imageWithCGImage:imageMasked];
}
@end
Replies
iTasks - Task Manager/Todo List
<a href="http://steaps.techaos.com/" targe
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI'm a bit of an Objective-C noob myself so I thought it would be nice to post helpful pieces of code that the community might benefit from.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAnd yes, it is nice to post helpful pieces of code for the community.
iTasks - Task Manager/Todo List
<a href="http://steaps.techaos.com/" targe
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSome brief questions and suggestions:
1> Where does the file go? What about cleanup, I'd hate to cache a few hundred images and then leave them in limbo?
I did a NSLOG and see the file goes here...
/var/folders/RJ/RJWwAcBZFtC36gaSUm0wKU+++TI/-Tmp-/ad.jpg
I tried to use finder to actually find the file to delete it, so I can test it again.
Suggestions or maybe post some additional help to extend the utilities class with two additional method (clearImage) to have a clear a single image (by filename) or the entire image cache (clearImageCache) ?
BTW - I updated your original code to take a filename arg. It seemed like you'd want a handle on that - for doing things like the forementioned cleanup..
Finally - here's an example of how to create and call your class:
// Assumes you've included the class in the header.
// ImageName is the url of an image (NSSTRING)
//imgView is the image (UIImageView)
Utilities *utils = [[Utilities alloc] init];
UIImage *tempImg = [utils getCachedImage:ImageName];
[imgView setImage:tempImg];
// ...
[Utilities release];
Again fab example!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAny idea this part:
NSString *filename = [[something unique, perhaps the image name]]
what code can i use in that box so that that the filename will be the original image name? thanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomesqlite is a database, there is no motive to cache a result from a database, generally a query need only few milliseconds.
for example, also with 100,000 records, derive the data (40-50 entry?) from a ID of a player, would take approximately less than 1 second.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI put
Any ideas?
Follow me on Twitter!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDont know if anybody is still looking this subject , but i ll give it a try...
I used chewbocka's tutorial...I added ns object named Utulities... i call it from cellForRowAtIndexPath: method, like this....
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *theURL = [[stories objectAtIndex:storyIndex] objectForKey:@"image"];
Utilities *utils = [[Utilities alloc] init];
UIImage *tempImg = [utils getCachedImage:theURL];
[cell.pic setImage:tempImg];
Its always shows the same parsed image...All table cells shows same pic :S
dont know what i'm doing wrong here :(
could anobody help me?
really thanks...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomethe reason it is always showing the same image is because the "storyIndex" isn't changing. It is always accessing the last object in the array.
instead of:
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *theURL = [[stories objectAtIndex:storyIndex]
objectForKey:@"image"];
try
NSString *theURL = [[stories objectAtIndex:indexPath.row] objectForKey:@"image"];
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThank you for your reply...i tried what you said..i did'nt get error but its still same..:(
i put NS logs to utulities.m methots and here whats going on...
- (UIImage *) getCachedImage: (NSString *) ImageURLString
{
NSString *filename = @"haberler3";
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
NSLog(@"Cashe");
image = [UIImage imageWithContentsOfFile: uniquePath]; // this is the cached image
}
else
{
// get a new one
[self cacheImage: ImageURLString];
NSLog(@"casheImage called");
image = [UIImage imageWithContentsOfFile: uniquePath];
}
return image;
}
and my console is like this,
2010-12-03 23:50:48.997 KP2[2376:207] ImageUrl: http://news.bbcimg.co.uk/media/images/50274000/jpg/_50274482_alcott66a.jpg
2010-12-03 23:50:48.997 KP2[2376:207] adding story: Alcott breaks leg in Lake Louise
2010-12-03 23:50:48.998 KP2[2376:207] add yaptı: http://news.bbcimg.co.uk/media/images/50274000/jpg/_50274482_alcott66a.jpg
2010-12-03 23:50:48.998 KP2[2376:207] date: Fri, 03 Dec 2010 06:57:29 GMT
2010-12-03 23:50:48.999 KP2[2376:207] ImageUrl: http://news.bbcimg.co.uk/media/images/50281000/jpg/_50281502_snow-leaopard.jpg
2010-12-03 23:50:48.999 KP2[2376:207] adding story: 'Snow Leopard' launches Ghana ski centre bid
2010-12-03 23:50:49.000 KP2[2376:207] add yaptı: http://news.bbcimg.co.uk/media/images/50281000/jpg/_50281502_snow-leaopard.jpg
2010-12-03 23:50:49.000 KP2[2376:207] date: Fri, 03 Dec 2010 13:15:28 GMT
2010-12-03 23:50:49.027 KP2[2376:207] Cashe
2010-12-03 23:50:49.031 KP2[2376:207] Cashe
2010-12-03 23:50:49.033 KP2[2376:207] Cashe
2010-12-03 23:50:49.035 KP2[2376:207] Cashe
2010-12-03 23:50:49.037 KP2[2376:207] Cashe
2010-12-03 23:50:53.839 KP2[2376:207] Cashe
2010-12-03 23:50:54.005 KP2[2376:207] Cashe
2010-12-03 23:50:54.255 KP2[2376:207] Cashe
2010-12-03 23:50:54.740 KP2[2376:207] Cashe
2010-12-03 23:50:54.972 KP2[2376:207] Cashe
2010-12-03 23:50:55.022 KP2[2376:207] Cashe
2010-12-03 23:50:55.072 KP2[2376:207] Cashe
2010-12-03 23:50:55.122 KP2[2376:207] Cashe
2010-12-03 23:50:55.189 KP2[2376:207] Cashe
2010-12-03 23:50:55.256 KP2[2376:207] Cashe
2010-12-03 23:50:55.822 KP2[2376:207] Cashe
dont know why its always calling cash... it looks like method doesnt passes to 'else' part :(
thank you for your help...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesometry this:
and insert a different "imageName" for each image you want to cache.
I hope this helps.
Peter
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomei finnally managed it to work...but it didn't work the way i though... Scrolling is laggy at first scroll...('till images downloaded) . i want images to be downloaded at start-up. Anyway, you were so helpfull... Thank you..
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThis lib makes downloading images a lot easier.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome[data release];
[image release];
in - (UIImage *)getImage: (NSString *) ImageURLString
cheers
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome-Cheers,
George
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDo you guys have a simple working code/project I can download that uses this tutorial? :)
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome