All pastes #946922 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-03-18 01:16 UTC
rendered paste body
-(int)flickrUploadPic:(NSData*) jpeg 
{
	NSAutoreleasePool* pool = [NSAutoreleasePool new];
	{
		NSMutableString *url=[NSMutableString stringWithString:@"http://api.flickr.com/services/upload"];
		NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
		
		NSURL *theURL = [NSURL URLWithString:url];
		
		NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1000.0f];
		[theRequest setHTTPMethod:@"POST"];
		
		NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",POSTDataSeparator];
		[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
		
		NSData* uploadData = prepareUploadData(jpeg, @"test.jpg", params, token);
		NSString* uploadDataStr =  [[NSString alloc] initWithData:uploadData encoding:NSASCIIStringEncoding];
		NSLog(@"Body is (%@)", uploadDataStr );
		[theRequest setHTTPBody:uploadData];
		
		NSURLResponse *theResponse = NULL;
		NSError *theError = NULL;
		NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
		NSString *theResponseString = [[NSString alloc] initWithData:theResponseData encoding:NSASCIIStringEncoding] ;
		NSLog(@"response  is (%@)", theResponseString);
		id errmsg = nil;
		
		NSXMLDocument *xmlDoc = [[NSClassFromString(@"NSXMLDocument") alloc] initWithXMLString:theResponseString options:NSXMLDocumentXMLKind error:&errmsg];
		NSXMLNode *stat =[[xmlDoc rootElement] attributeForName:@"stat"];
		
		if (![[stat stringValue] isEqualToString:@"ok"]) 
		{ 
			//[alertSheet setBodyText:[NSString stringWithFormat:@"Failed to send pic. Check authentications. Code(%@)", [stat stringValue]]];
			//[alertSheet popupAlertAnimated:YES];
			[status setText:[NSString stringWithFormat:@"Failed to send pic. Check authentications."]];
			[lock lock];
			uploadQSize--;
			[lock unlock];
			return 0;
			
		}	
		[lock lock];
		uploadQSize--;
		if(uploadQSize <= 0)
		{
			NSLog(@"Stoping animation\n");
			[status removeFromSuperview];
			[progress stopAnimation];
		}
		if(uploadQSize > 0)
		{
			[status setText:[NSString stringWithFormat:@"Sending %d pics", uploadQSize]];
		}
		[lock unlock];
		
	}
	[pool release];
	return 1;
}