我尝试在iPhone上创建一个Web服务器,以便在ablum中提供图像的URL。 在向客户端发送响应之前,我需要对图像进行一些调整(类似于重新调整大小)。 在响应客户端之前,这些操作每个映像最多需要1或1.5秒。 (特别是在iPhone 4上) 客户端无法接收数据,即使xcode控制台上的消息已经显示某些套接字上已经有多少字节GET。 我可以用什么方法来解决这个问题?
另一个问题是如果我将ConnectedStateCoalescingInterval值增加到2.0或3.0秒会发生什么?
感谢。
========= 2015/08/24 13:05更新
这是我的addHandlerForMethod:path:requestClass:asyncProcessBlock:
[_webServer addHandlerForMethod:@"GET" path:[NSString stringWithFormat:@"/image/%@",assetId] requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(GCDWebServerRequest *request, GCDWebServerCompletionBlock completionBlock)
{
__strong typeof(weakself) strongself = weakself;
[strongself requestImageDataByAssetURL:assetURL completionCb:^(NSData *photoData) {
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithData:photoData contentType:@"image/png"];
completionBlock(response);
}];
}];
这是requestImageDataByAssetURL:completionCb:
- (void)requestImageDataByAssetURL:(NSURL *)assetURL completionCb:(void(^)(NSData *photoData))cbfunc {
__block NSData *photoData;
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
@autoreleasepool {
uint64_t begin = mach_absolute_time();
// <do some image processing here>
uint64_t end = mach_absolute_time();
NSLog(@"Time taken to imageResize %g s", MachTimeToSecs(end - begin));
cbfunc(photoData);
}
} failureBlock:^(NSError *error) {
NSLog(@"[%@] fail. Error=%@", NSStringFromSelector(_cmd), error.localizedDescription);
cbfunc(nil);
}];
}
这是日志:
[DEBUG] [2015-08-24 04:48:09 +0000] Did open connection on socket 32
[DEBUG] Connection received 221 bytes on socket 32
[DEBUG] Connection on socket 32 preflighting request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection on socket 32 processing request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] [2015-08-24 04:48:10 +0000] Did open connection on socket 34
2015-08-24 12:48:10.799 xBoard[2981:3707] Time taken to imageResize 1.52039 s
[DEBUG] Connection received 221 bytes on socket 34
[DEBUG] Connection on socket 34 preflighting request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection on socket 34 processing request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection sent 171 bytes on socket 32
[DEBUG] Connection sent 123575 bytes on socket 32
[DEBUG] Did close connection on socket 32
[VERBOSE] [172.16.20.174:8080] 172.16.21.97:34450 200 "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" (221 | 123746)
2015-08-24 12:48:12.028 xBoard[2981:4f0b] Time taken to imageResize 1.06382 s
[DEBUG] Connection sent 171 bytes on socket 34
[DEBUG] Connection sent 123575 bytes on socket 34
[DEBUG] Did close connection on socket 34
[VERBOSE] [172.16.20.174:8080] 172.16.21.97:50852 200 "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" (221 | 123746)
我们可以看到它创建了第一个套接字(32) 然后立即为同一图像请求创建第二个套接字(34) 看来GCDWebServer立即关闭了第一个套接字。但为什么呢?
P.S。顺便说一下,客户端使用Android Volley库来下载图像而不是使用Web浏览器。
答案 0 :(得分:0)
根据日志似乎没有任何错误:路径GET
的2 /image/9D959040-9FA8-4197-8150-8DC72339955D
个请求在套接字32和34上处理,每个返回123,575个字节。
尝试使用Chrome及其网络检查器等桌面网络浏览器。如果一切正常,那么问题在于您的Android应用。