Search code examples
iosobjective-cnsdatafmdb

UIImagePNGRepresentation function return back NSData type,whether it can be stored in SQLite?


- (void)addRecordImageData:(NSData *)imageData x:(CGFloat)x y:(CGFloat)y cutterImageName:(NSString *)name
{
    NSInteger imageID = 0;
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select id from imageName where name = '%@'",name];
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            imageID = [result intForColumn:@"id"];
        }
        [db close];
    }
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into imageData(nameid,data,x,y) values (%d,%@,%.f,%.f)",imageID,imageData,x,y];
        BOOL success = [db executeUpdate:sql];
        success ? NSLog(@"insert success") : NSLog(@"insert faulire");
        [db close];
    }
}

- (void)setUp:(UIImage*)cutterImage name:(NSString *)name
{
    CGImageRef imageRef = cutterImage.CGImage;
    totalColumns = ceilf(cutterImage.size.width / CUTWIDTH *1.0);
    totalRows = ceilf(cutterImage.size.height / CUTHEIGHT *1.0);
    NSInteger x=0,y=0;
    for (y = 0; y<totalRows; y++) {
        NSMutableArray *row = [NSMutableArray array];
        for (x = 0; x < totalColumns; x++) {
            CGFloat xOffset = x * CUTWIDTH;
            CGFloat yOffset = y * CUTHEIGHT;
            CGImageRef tileImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(xOffset, yOffset, CUTWIDTH, CUTHEIGHT));
            NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:tileImageRef]);
            
            [self.dataBase addRecordImageData:imageData x:xOffset y:yOffset cutterImageName:name];
        }
    }

}

this is my code,When I insert the data into the database, it is always a failure.I use fmdb.When I run the program, automatically jump to a section of code in fmdb.


Solution

  • I think the problem is in your insert query. It should be somewhat like this

    @"insert into imageData(nameid,data,x,y) values (?,?,?,?)" 
    

    Checkout this for more.