如何使用完整数据将UIImage写入NSOutputStream

时间:2014-11-27 07:11:15

标签: ios objective-c

如何使用完整数据将UIImage发送到NSOutputStream。 我已经尝试了这段代码但输出没有写完整的图像。如何使用完整的图像数据发送到输出。

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
  NSData *imgData = UIImageJPEGRepresentation(image, 0.2);

    NSMutableData *completeData = [[NSMutableData alloc] initWithBytes:[stringData bytes] length:[stringData length]];
    [completeData appendData:imgData];

    NSInteger bytesWritten = 0;
    while ( imgData.length > bytesWritten )
    {
      while ( ! self.outputStream.hasSpaceAvailable )
           [NSThread sleepForTimeInterval:0.05];

        //sending NSData over to server
        NSInteger writeResult = [self.outputStream write:[imgData bytes]+bytesWritten maxLength:[imgData length]-bytesWritten];
        if ( writeResult == -1 )
            NSLog(@"error code here");
       else
            bytesWritten += writeResult;
    }
}

1 个答案:

答案 0 :(得分:2)

您只发送imgData

  NSData *imgData = UIImageJPEGRepresentation(image, 0.2);

    NSMutableData *completeData = [[NSMutableData alloc] initWithBytes:[stringData bytes] length:[stringData length]];
    [completeData appendData:imgData];

    NSInteger bytesWritten = 0;
    while ( completeData.length > bytesWritten )
    {
      while ( ! self.outputStream.hasSpaceAvailable )
           [NSThread sleepForTimeInterval:0.05];

        //sending NSData over to server
        NSInteger writeResult = [self.outputStream write:[completeData bytes]+bytesWritten maxLength:[completeData length]-bytesWritten];
        if ( writeResult == -1 )
            NSLog(@"error code here");
       else
            bytesWritten += writeResult;
    }