简体   繁体   English

如何将UIImage与完整的数据写入NSOutputStream

[英]How to write UIImage to NSOutputStream with complete data

How to send UIImage to NSOutputStream with complete data. 如何将UIImage和完整的数据发送到NSOutputStream。 I have try this code but output not write complete image. 我尝试了此代码,但输出未写入完整的图像。 How to send to output with complete data of image. 如何发送带有完整图像数据的输出。

- (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;
    }
}

You are sending only imgData 您只发送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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM