【问题标题】:UIImagePNGRepresentation function return back NSData type,whether it can be stored in SQLite?UIImagePNGRepresentation 函数返回 NSData 类型,是否可以存储在 SQLite 中?
【发布时间】:2015-11-13 02:26:58
【问题描述】:

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

}

这是我的代码,当我往数据库中插入数据时,总是失败。我用的是fmdb。当我运行程序时,自动跳转到fmdb中的一段代码。

【问题讨论】:

    标签: ios objective-c nsdata fmdb


    【解决方案1】:

    我认为问题出在您的插入查询中。 应该是这样的

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

    结帐this 了解更多信息。

    【讨论】:

    • 插入数字的正确方法是把它装箱到一个 NSNumber 对象中
    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 2017-10-17
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2018-03-18
    • 2021-08-30
    相关资源
    最近更新 更多