2011-08-26 82 views
-1

您好我正在激发一个交易,但由事务结果一个结果来作为blob属性,即图像,我想改变该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

千万不要使用description的输出做处理。它的格式不能保证。什么格式是你原来的“blob”,它是如何产生的?你的代码表明它可能是NSData或者它可能是NSKeyArchiver。这两个很容易转换为NSData。您无需手动转换为字符串即可完成此操作。

相关问题