#import <UIKit/UIKit.h>
@interface ImageUploadViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
IBOutlet UIImageView *imageView;
UIImagePickerController *pickerController;
}
@property(nonatomic,retain)UIImageView *imageView;
-(IBAction)uploadImage;
-(IBAction)selectedImage;
@end
//
// ImageUploadViewController.m
// ImageUpload
//
// Created by wangqiulei on 9/2/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "ImageUploadViewController.h"
@implementation ImageUploadViewController
@synthesize imageView;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//[pickerController release];
//pickerController.delegate=self;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
//-(void) viewWillAppear:(BOOL)animated{
//[self.view setNeedsDisplay];
//}
-(void) uploadImage{
//获取当前所使用的设备
UIDevice *dev=[UIDevice currentDevice];
//得到设备标识符
NSString *uniqueId=dev.uniqueIdentifier;
//设置图片名字 photo1
NSString *name=@"photo1";
//UIImage *image=[UIImage imageNamed:@"5.jpg"];
NSData *imageData=UIImageJPEGRepresentation(imageView.image, 90);
//创建服务器连接地址
NSString *urlString = [@"http://localhost:8080/upload/?" stringByAppendingString:@"uid="];
urlString = [urlString stringByAppendingString:uniqueId];
urlString = [urlString stringByAppendingString:@"&name="];
urlString = [urlString stringByAppendingString:name];
urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];
NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//postBody封装所要传的图片数据
NSMutableData *postBody=[NSMutableData data];
[postBody appendData:imageData];
[request setHTTPBody:postBody];
//向服务器发送请求
NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
-(void) selectedImage{
pickerController=[[UIImagePickerController alloc] init];
pickerController.delegate=self;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:pickerController animated:YES];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
imageView.image=image;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[imageView release];
[super dealloc];
}
@end