apple8
apple8

Reputation: 33

How to write UIImage to NSOutputStream with complete data

How to send UIImage to NSOutputStream with complete data. 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;
    }
}

Upvotes: 0

Views: 320

Answers (1)

l0gg3r
l0gg3r

Reputation: 8954

You are sending only 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;
    }

Upvotes: 2

Related Questions