0
我需要上傳圖片到web服務。以下是我返回上傳圖片的代碼段。圖像尺寸非常大(約6Mb)。我使用GCD在後臺線程中上傳該圖像。在後臺線程中上傳大圖。
if([VSCore ConnectedToInternet ])
{
bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
//[application endBackgroundTask:self->bgTask];
//self->bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[vrs write:data toURI:URI];
[[UIApplication sharedApplication]endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//
}
-(BOOL)write:(NSData *)data toURI:(NSString *)URI
{
BOOL retVal = NO;
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [data length]];
NSRange range = [URI rangeOfString:@"http"];//Is http?
if(range.location != NSNotFound)
{
//Yes, http
NSMutableURLRequest *httpRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URI]];
[httpRequest setHTTPMethod:@"POST"];
[httpRequest setHTTPBody:data];
[httpRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[httpRequest setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:httpRequest delegate:self];
[theConnection release];
[httpRequest release];
if (theConnection)
{
receivedData=[[NSMutableData data] retain];
retVal = YES;
}
else
{
NSError *error = [NSError alloc];
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
[error release];
retVal = NO;
}
}
return retVal;
}
現在我面臨的問題是,如果我嘗試上傳背景圖片線程請求不會服務器(我正在檢查服務器上的日誌文件)。但如果我在主線程中上傳圖像,請求將發送到服務器(僅用於測試目的,我知道在主線程中上傳大圖像不是個好主意)。那麼我在這裏做錯了什麼?背景線程有什麼問題嗎? Plz幫助我。提前致謝。
,如果我上傳的圖片一樣,你說,它不會引起我的UI,以阻止了一段時間? – RockandRoll 2013-04-09 11:47:00