2011-08-26 116 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。您無需手動轉換爲字符串即可完成此操作。

相關問題