将图像发送到服务器,图像未格式化

时间:2013-11-07 15:27:56

标签: objective-c sockets uiimage

我正在尝试通过套接字将图像发送到服务器,但图像在服务器端不能很好地显示。

我正在使用此代码,可能是什么问题?我正在向服务器发送文本并且它可以工作,但图像看起来没有格式化。

    UIImage *imageResized=[self resizeImage:editedImage newSize:CGSizeMake(64,32)];

    NSData *imageData = UIImageJPEGRepresentation(imageResized, 1.0f); 

    NSString *encodedString = [imageData base64EncodedStringWithOptions:nil];  

    NSString *finalStr= [NSString stringWithFormat:@"image:%@",encodedString];

    NSData* data = [finalStr dataUsingEncoding:NSASCIIStringEncoding];

    [tcpsocket initNetworkCommunication:[tcpSocket linkWebserviceInet1] porta:[tcpSocket linkWebport]];
    [tcpsocket sendNSData:data];

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 

    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();

    return newImage;
}

0 个答案:

没有答案