从字节数组中获取UIImage来自.Net Web服务

时间:2012-08-28 16:42:16

标签: iphone ios uiimage

我的问题与Converting byte array coming from Web service to UIImage iPhone相同 。现在我将这些字节存储在NSMutableArray中。但方法是:

NSData *data = [NSData dataWithBytes:YOUR_BYTE_ARRAY length:ARRAY_LENGTH];

将arrayOfBytes作为参数。任何人都可以告诉我如何在字节数组中转换此数组。我搜索了很多但无法找到相关内容。

2 个答案:

答案 0 :(得分:1)

不确定你是如何开始使用可变数组的。如果您正在使用NSURLConnection,则委托将获得NSData,因此您无需使用可变数组。考虑使用像这样的连接异步块方法获取数据......

NSURLRequest *myRequest = // the request you've already got working to get image data
[NSURLConnection sendAsynchronousRequest:myRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (!error) {
        // image from data with no intermediate mutable array or byte array
        UIImage *image = [UIImage imageWithData:data];
    }
 }];

答案 1 :(得分:1)

感谢您的合作。但它对我没有帮助。经过长时间的研究,我找到了解决方案。我正在分享我的信息,以便其他人得到正确的答案。

        NSArray *byteArray = [[NSArray alloc]init]; //strore all data here coming from server in byte formate

        unsigned c = [byteArray count];
        uint8_t *bytes = malloc(sizeof(*bytes) * c);

        unsigned i;
        for (i = 0; i < c; i++)
        {
            NSString *str = [byteArray objectAtIndex:i];
            int byte = [str intValue];
            bytes[i] = (uint8_t)byte;
        }

        NSData *data = [[NSData alloc]initWithBytes:bytes length:c];
        UIImage *image = [UIImage imageWithData:data];
        NSLog(@"image %@",image);