Graphics Programs Reference
In-Depth Information
[d writeToFile:imagePath atomically:YES];
}
Let's examine this code more closely. The function UIImageJPEGRepresentation
takes two parameters: a UIImage and a compression quality. The compression quality is
a float from 0 to 1, where 1 is the highest quality. The function returns an instance of
NSData .
This NSData instance can be written to the filesystem by sending it the message
writeToFile:atomically: . The bytes held in this NSData are then written to the
path specified by the first parameter. The second parameter, atomically , is a Boolean
value. If it is YES , the file is written to a temporary place on the filesystem, and, once the
writing operation is complete, that file is renamed to the path of the first parameter, repla-
cing any previously existing file. Writing atomically prevents data corruption should your
application crash during the write procedure.
It is worth noting that this way of writing data to the filesystem is not archiving. While
NSData instances can be archived, using the method writeToFile:atomically:
is a binary write to the filesystem.
In BNRImageStore.m , make sure that when an image is deleted from the store, it is
also deleted from the filesystem:
- (void)deleteImageForKey:(NSString *)s
{
if (!s)
return;
[dictionary removeObjectForKey:s];
NSString *path = [self imagePathForKey:s];
[[NSFileManager defaultManager] removeItemAtPath:path
error:NULL];
}
Now that the image is stored in the filesystem, the BNRImageStore will need to load
that image when it is requested. The class method imageWithContentsOfFile: of
UIImage will read in an image from a file, given a path.
In BNRImageStore.m , replace the method imageForKey: so that the
BNRImageStore will load the image from the filesystem if it doesn't already have it.
- (UIImage *)imageForKey:(NSString *)s
{
return [dictionary objectForKey:s];
Search WWH ::




Custom Search