通过 NSOutputStream 发送 UIImage [英] Sending UIImage over NSOutputStream
问题描述
我正在尝试将用户拍摄的图像发送到服务器.我得到 JPEG 表示,将其附加到上传照片所需的字符串,然后通过 NSOutputStream 发送数据.但是,当我尝试从服务器取回照片时,我只能从顶部看到其中的 10%.任何帮助将不胜感激.
I'm trying to send an image that user takes to the server. I get the JPEG representation, append that to the string needed to upload the photo, and then send the data through NSOutputStream. However, when I try to get the photo back from server, I only see 10% of it from the top. Any help would be appreciated.
顺便说一句.套接字已打开并已连接.
btw. socket is open and connected.
这是我的代码:
NSString *requestString = [NSString stringWithFormat:@"SubmitPhoto::%@::", userID];
NSData * stringData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
NSMutableData *completeData = [[NSMutableData alloc] initWithBytes:[stringData bytes] length:[stringData length]];
[completeData appendData:imgData];
//sending NSData over to server
[self.outputStream write:[completeData bytes] maxLength:[completeData length]];
推荐答案
这是因为您的图片大小限制被超出.
It's because of your image size limit is being exceeded.
更好的处理方法是实现以下逻辑.
Better way to handle this is to implement the following logic.
发件人
将
UIimage
转换为NSData
将 NSData
拆分为不同的块(建议每个块 1024 个)
Split up the NSData
to different chunks (1024 per chunk is recommended)
发送 &跟踪 NSData
Send & track each chunk of NSData
接收方
声明
NSData
并将接收到的NSData
块的第一部分(1024)存储到其中.
Declare
NSData
and Store the first part ofNSData
chunk (1024) into it, which is received.
接收NSData
的下一个块并使用appendData:
方法附加它
Receive the next chunks of NSData
and make use appendData:
method to append it
接收到所有数据块后,将接收到的 NSData
转换为 UIImage
Once all the chunks are received, convert the received NSData
as an UIImage
确保设计不同的结构以将数据作为块传输,例如表示详细信息的结构(总块、总大小、块大小等)、表示数据的结构(当前块大小、当前块编号等)..),表示响应数据的结构(交付状态、交付的块号等.).
Make sure to design different structures for transferring the data as chunks such as structure to represent the details (total chunk, total size, chunk size etc..), structure to represent the data (current chunk size, current chunk number etc..), structure to represent the responds data (delivery status,chunk number delivered etc..).
这篇关于通过 NSOutputStream 发送 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!