Make Better Things



I like to make better things.

image caching in iphone sdk

If you’re pulling in web data such as images that doesn’t change but needs to be viewed multiple times, you’ll want to cache it. Caching images dramatically improves the performance of scrolling tables and other views.

Here is a very simple class, with which you can implement caching of images in your iPhone app.

ImgCache.h :


#import <UIKit/UIKit.h>

@interface ImgCache : NSObject {

}

- (void) cacheImage: (NSString *) ImageURLString;
- (UIImage *) getImage: (NSString *) ImageURLString;

@end

ImgCache.m :


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

@implementation ImgCache

- (void) cacheImage: (NSString *) ImageURLString
{
 NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
 //generating unique name for the cached file with ImageURLString so you can retrive it back
 NSMutableString *tmpStr = [NSMutableString stringWithString:ImageURLString];
 [tmpStr replaceOccurrencesOfString:@"/" withString:@"-" options:1 range:NSMakeRange(0, [tmpStr length])];

 NSString *filename = [NSString stringWithFormat:@"%@",tmpStr];

 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];

 // 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 *) getImage: (NSString *) ImageURLString
{
 NSMutableString *tmpStr = [NSMutableString stringWithString:ImageURLString];
 [tmpStr replaceOccurrencesOfString:@"/" withString:@"-" options:1 range:NSMakeRange(0, [tmpStr length])];

 NSString *filename = [NSString stringWithFormat:@"%@",tmpStr];
 NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];

 UIImage *image;

 // Check for a cached version
 if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
 {
 NSData *data = [[NSData alloc] initWithContentsOfFile:uniquePath];
 image = [[UIImage alloc] initWithData:data] ; // this is the cached image
 }
 else
 {
 // get a new one
 [self cacheImage: ImageURLString];
 NSData *data = [[NSData alloc] initWithContentsOfURL:uniquePath];
 image = [[UIImage alloc] initWithData:data];

 }

 return image;
}


@end

installing zend debugger on xampp and setting it up with zend studio

If you have XAMPP installed, the Zend Optimizer will be installed by default, and this causes the standard installation of Zend Debugger to fail. Follow the steps below for success.

1) Make sure you have a running installation of XAMPP. Installing XAMPP is not part of this article. You can test your installation by entering http://localhost/xampp/ in a browser.

2) Enter http://localhost/xampp/phpinfo.php in the address bar of your browser. Note the information about Zend Extension Manager and Zend Optimizer. By the end of this article, Zend Debugger will be added.

3) Download the latest .zip file for cygwin_nt-i386 from http://downloads.zend.com/pdt/server-debugger/ and unpack into xamppphpzendDebuggerlibDebugger. Caution: In a standard XAMPP installation, the instructions in the README.txt file in this download will cause Apache not to start. See the following steps instead.

4) Rename the the subdirectories from “4_3_x_comp” etc. to “php-4.3.x” etc. see image.

install-zend-debugger-xampp

Rename directories

5) Copy the file dummy.php in the download to your Apache htdocs directory, for example xampphtdocsdummy.php. This what all the instructions and help files say, but I forgot it on one installation and didn’t notice any problems. It can’t hurt to do it though.

6) Search the PHPInfo that you ran in step 2 for the value “Loaded Configuration File”. Open this php.ini file in a text editor. You might want to save a copy of it before editing it.

7) If you have XAMPP you will find some lines in php.ini that look like this:

[Zend]
zend_extension_ts = “G:xamppphpzendOptimizerlibZendExtensionManager.dll”
zend_extension_manager.optimizer_ts = “G:xamppphpzendOptimizerlibOptimizer”
zend_optimizer.enable_loader = 0
zend_optimizer.optimization_level=15

;zend_optimizer.license_path =
; Local Variables:
; tab-width: 4
; End:

8 ) If you see the lines in step 7 and follow the instructions from README.txt in the download in step 3, you will be adding a second zend_extension_ts line. This will cause Apache not to start. Instead, add the following lines:
zend_extension_manager.debug_server_ts = “xamppphpzendDebuggerlibDebugger”
zend_debugger.allow_hosts = 127.0.0.1
zend_debugger.expose_remotely = allowed_hosts

9) Restart XAMPP, for example by calling xamppxampp_restart.exe. Reload PHPInfo; now it should look like this new image. Note the new information about Zend Debugger.

zend debugger

zend debugger