Blob到图像转换

时间:2011-08-26 14:03:56

标签: objective-c

我通过触发事务获取结果但是通过事务一个结果将作为blob属性,即图像,我想将该blob属性更改为图像 我为“图标”编写的代码是从事务中获取图像的关键, 所以请帮我看一下, 图像打印为零, 为什么呢?

NSString *inputString = [[[self formModel] attributeAsString:@"icon"] description];

NSLog(@"icon is %@",[[self formModel] attributeAsString:@"icon"]);

NSLog(@"inputstring is %@",inputString);
//NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[self formModel] attributeAsString:@"icon"]];
//NSLog(@"image is %@",image);

NSArray *words = [inputString componentsSeparatedByString:@" "];

NSLog(@"words is %@",words);

NSArray *sizes = [words valueForKey:@"length"];
int sizeOfBytes = 0;
for (NSNumber *size in sizes) {
    sizeOfBytes += [size intValue]/2;
}
int bytes[sizeOfBytes];
int counts = 0;
for (NSString *word in words) {
    // convert each word from string to int
    NSMutableString *ostr = [NSMutableString stringWithCapacity:[word length]];
    while ([word length] > 0) {
        [ostr appendFormat:@"%@", [word substringFromIndex:[word length] - 2]];
        word = [word substringToIndex:[word length] - 2];
    }

    NSScanner *scaner = [NSScanner scannerWithString:ostr];
    unsigned int val;
    [scaner scanHexInt:&val];
    bytes[counts] = val;
    counts++;
}
// get NSData form c array
NSData *data = [NSData dataWithBytes:bytes length:sizeOfBytes];
NSLog(@"My NSDATA  %@",data);
NSImage *Image = [[NSImage alloc] initWithData:data];

1 个答案:

答案 0 :(得分:1)

切勿使用description的输出进行处理。无法保证其格式。你的原始“blob”是什么格式以及它是如何生成的?您的代码表明它可能是NSData,也可能是NSKeyArchiver。这两个都很容易转换为NSData。您永远不需要通过转换为字符串手动执行此操作。