【问题标题】:Make UIImage from ByteArray getting from .Net WebService从 .Net WebService 获取 ByteArray 的 UIImage
【发布时间】:2012-08-28 16:42:16
【问题描述】:

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

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

以arrayOfBytes 作为参数。所以谁能告诉我如何将此数组转换为字节数组。查了很多,没找到相关内容。

【问题讨论】:

  • 你是如何将原始字节存储在 NSMutableArray 中的?

标签: iphone ios uiimage


【解决方案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];
    }
 }];

【讨论】:

    【解决方案2】:

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

            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);
    

    【讨论】:

    • 完美!这就是应该这样做的方式。也为我工作。在这上面敲了我近 4 个小时的头。感谢分享。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 2011-12-15
    • 2014-08-05
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    相关资源
    最近更新 更多