We have a sqlite database that our iOS app stores images in in a blob column. We use FMDB to read the blob as NSData and then convert to a UIImage. The code is shown below.
-(UIImage*)getImageWithGuid:(NSString *)guid imageSizeKind:(ImageSizeKind)imageSizeKind
{
FMDatabase *db = [self openFMDatabase];
if (!db) {
return nil;
}
NSData *imageData = nil;
NSString *query = [NSString stringWithFormat:@"SELECT Image FROM images WHERE Guid = '%@' AND MediaType = %d limit 1", guid, imageSizeKind];
FMResultSet *rs = [db executeQuery:query];
if ([rs next])
{
imageData = [rs dataForColumn:imagesTable.image];
}
[rs close];
[db close];
if (!imageData) {
NSLog(@"Image was not found in database '%@' using sql query '%@'", [self databasePath], query);
}
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
The caller to this method above, receives the image and then resizes it. In the caller to this method above, I have some code to time the obtaining and resizing portions of the code, and I received the following output in the debug console...
23:31:17.084 Obtained image in 4.208354 sec
23:31:17.086 Resized image in 0.001961 sec
23:31:17.115 Obtained image in 0.028943 sec
23:31:17.117 Resized image in 0.001891 sec
23:31:17.131 Obtained image in 0.013373 sec
23:31:17.133 Resized image in 0.002036 sec
23:31:17.844 Obtained image in 0.711072 sec
23:31:17.846 Resized image in 0.001634 sec
23:31:17.880 Obtained image in 0.034076 sec
23:31:17.882 Resized image in 0.001678 sec
23:31:17.910 Obtained image in 0.028255 sec
23:31:17.912 Resized image in 0.001652 sec
23:31:17.943 Obtained image in 0.031323 sec
23:31:17.945 Resized image in 0.001783 sec
23:31:17.954 Obtained image in 0.009396 sec
23:31:17.956 Resized image in 0.001982 sec
23:31:17.986 Obtained image in 0.029724 sec
23:31:17.988 Resized image in 0.001977 sec
23:31:18.026 Obtained image in 0.037283 sec
23:31:18.027 Resized image in 0.001837 sec
23:31:18.051 Obtained image in 0.023700 sec
23:31:18.053 Resized image in 0.001947 sec
23:31:18.088 Obtained image in 0.035087 sec
23:31:18.090 Resized image in 0.001687 sec
23:31:18.136 Obtained image in 0.045304 sec
Notice that the very first image took a whopping 4.2 seconds to obtain, then all the following images took mere hundredths of a second.
Is there some way that I can "prime the pump" so-to-speak to get that 4.2 seconds out of the way and have the database ready to behave like it did with all the images that followed. Ideally, it would be awesome to tuck that 4 second delay away on some background thread so that the user doesn't have to experience it at some other point in the app, simply by moving this initial 4 seconds somewhere else to be incurred.
Thanks.
Aucun commentaire:
Enregistrer un commentaire