我正在开发一个 iPhone 应用程序,该应用程序涉及将相机中的完整照片(每张通常在 1.5 到 2.0 MB 之间)及其缩略图(小得多)上传到 Amazon S3。
缩略图总是会成功上传,但有时完整的图像不会,当它们失败时,它们会失败并显示 POSIX 错误代码 12,又名 ENOMEM。但是,我添加了调试代码以在发生错误时打印可用内存量,并且总是有相当多的可用内存,通常超过 100 MB。
此外,当通过 3G 进行上传时,错误会更频繁地出现,而通过 wifi 时则更少——这看起来很奇怪,因为请求没有下载太多,并且正在上传的文件已经在内存中(我'我也尝试过从磁盘流式传输它,但没有任何改进)。
我已经尝试使用 NSURLConnection、Foundation CFHTTP* 函数和 ASIHTTPRequest 库上传文件,但无论如何,错误发生的频率相同。更奇怪的是,我的所有谷歌搜索都揭示了最终用户有时会从 Safari 获得错误代码 12——我还没有看到任何 iOS 开发人员提到它。我正在使用继承的代码库,所以它可能有问题,但我什至不确定要查找什么。任何见解将不胜感激!
最佳答案
我能够解决此问题的唯一方法是直接使用套接字并手动形成 HTTP header 。所以我的上传代码目前看起来像这样:
- (void)socketClose
{
[_inputStream setDelegate:nil];
[_inputStream close];
[_inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
SCR_RELEASE_SAFELY(_inputStream);
[_outputStream setDelegate:nil];
[_outputStream close];
[_outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
SCR_RELEASE_SAFELY(_outputStream);
SCR_RELEASE_SAFELY(_headerBuffer);
}
- (void)sendRequest
{
[self socketClose];
SCR_RELEASE_SAFELY(_headerBuffer);
if (!_shouldCancel)
{
NSString *httpMessage = [NSString stringWithFormat:@"POST upload.php HTTP/1.1\r\n"
"Host:"
#ifndef TESTBED
" %@"
#endif
"\r\n"
"User-Agent: MyApp/3.0.0 CFNetwork/534 Darwin/10.7.0\r\n"
"Content-Length: %d\r\n"
"Accept: */*\r\n"
"Accept-Language: en-us\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Connection: keep-alive\r\n\r\n"
"data="
#ifndef TESTBED
, [self.serverUrl host]
#endif
, _bytesToUpload];
NSString *key = @"data=";
NSData *keyData = [key dataUsingEncoding:NSASCIIStringEncoding];
_bytesToUpload -= [keyData length];
_bytesToUpload = MAX(0, _bytesToUpload);
_headerBuffer = [[NSMutableData alloc] initWithData:[httpMessage dataUsingEncoding:NSUTF8StringEncoding]];
_writtenDataBytes = 0;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault
, (CFStringRef)[self.serverUrl host]
#ifdef TESTBED
, 8888
#else
, 80
#endif
, (CFReadStreamRef *)(&_inputStream)
, (CFWriteStreamRef *)(&_outputStream));
[_inputStream setDelegate:self];
[_outputStream setDelegate:self];
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_inputStream open];
[_outputStream open];
}
}
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
if (_outputStream == theStream)
{
switch (streamEvent)
{
case NSStreamEventOpenCompleted:
{
[self regenerateTimeoutTimer];
break;
}
case NSStreamEventHasSpaceAvailable:
{
SCR_RELEASE_TIMER(_timeoutTimer);
NSInteger length = _headerBuffer.length;
if (length > 0)
{
NSInteger written = [_outputStream write:(const uint8_t *)[_headerBuffer bytes] maxLength:length];
NSInteger rest = length - written;
if (rest > 0)
{
memmove([_headerBuffer mutableBytes], (const uint8_t *)[_headerBuffer mutableBytes] + written, rest);
}
[_headerBuffer setLength:rest];
}
else
{
const uint8_t *dataBytes = [_data bytes];
while ([_outputStream hasSpaceAvailable] && (_writtenDataBytes < _bytesToUpload))
{
NSInteger written = [_outputStream write:dataBytes
maxLength:MIN(_dataLength, _bytesToUpload - _writtenDataBytes)];
if (written > 0)
{
_writtenDataBytes += written;
}
}
}
[self regenerateTimeoutTimer];
break;
}
case NSStreamEventErrorOccurred:
{
SCR_RELEASE_TIMER(_timeoutTimer);
[self reportError:[theStream streamError]];
break;
}
case NSStreamEventEndEncountered:
{
SCR_RELEASE_TIMER(_timeoutTimer);
[self socketClose];
break;
}
}
}
else if (_inputStream == theStream)
{
switch (streamEvent)
{
case NSStreamEventHasBytesAvailable:
{
SCR_RELEASE_TIMER(_timeoutTimer);
/* Read server response here if you wish */
[self socketClose];
break;
}
case NSStreamEventErrorOccurred:
{
SCR_RELEASE_TIMER(_timeoutTimer);
[self reportError:[theStream streamError]];
break;
}
case NSStreamEventEndEncountered:
{
SCR_RELEASE_TIMER(_timeoutTimer);
[self socketClose];
break;
}
}
}
}
虽然 ASIHTTPRequest 可以在这里工作,但我们决定摆脱这种依赖性,既为了获得性能,又为了让一切都在我们自己的准确控制之下。您可以使用 Wireshark 工具来调试此类内容。
关于iphone - 从 iPhone 上传文件时出现 POSIX 错误 12 ("Cannot allocate memory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542459/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po