//
// ViewController.h
// Xcode_FileUpload
//
// Created by KirSsu Ryu on 12-8-7.
// Copyright (c) 2012年 __JModule__. All rights reserved.
//
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize myWebView;
@synthesize picker_library_;
- (void)viewDidLoad
{
[super viewDidLoad];
//代理UIWebViewDelegate
myWebView.delegate = self;
//设置要载入的链接
NSURL *url = [NSURL URLWithString:@"http://*****/test/index.php"];
//创建一个请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//把请求发送到webView里,实现显示内容
[myWebView loadRequest:request];
}
- (void)viewDidUnload
{
[self setMyWebView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
//获得从网站得到的值
#pragma mark --
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//获得请求的URL,第一次是路径,当在web上点击按钮后获得的是web穿过来的路径。
NSString *requestString = [[request URL] absoluteString];
//根据":"拆分字符串,返回数组。
NSArray *components = [requestString componentsSeparatedByString:@":"];
//如果数组内的元素大于1并且第一个元素的值相等
if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"gallery"]) {
if([(NSString *)[components objectAtIndex:1] isEqualToString:@"open"])
{
[self openGallery];
}
return NO;
}
return YES;
}
//打开相册
-(void)openGallery{
//初始化类
picker_library_ = [[UIImagePickerController alloc] init];
//指定几总图片来源
//UIImagePickerControllerSourceTypePhotoLibrary:表示显示所有的照片。
//UIImagePickerControllerSourceTypeCamera:表示从摄像头选取照片。
//UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示仅仅从相册中选取照片。
picker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//表示用户可编辑图片。
picker_library_.allowsEditing = YES;
//代理
picker_library_.delegate = self;
[self presentModalViewController: picker_library_
animated: YES];
}
//3.x 用户选中图片后的回调
- (void)imagePickerController: (UIImagePickerController *)picker
didFinishPickingMediaWithInfo: (NSDictionary *)info
{
NSLog(@"3.x");
//获得编辑过的图片
UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];
[self dismissModalViewControllerAnimated:YES];
[self imageUpload:image];
}
//2.x 用户选中图片之后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSLog(@"2.x");
NSMutableDictionary * dict= [NSMutableDictionary dictionaryWithDictionary:editingInfo];
[dict setObject:image forKey:@"UIImagePickerControllerEditedImage"];
//直接调用3.x的处理函数
[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}
// 用户选择取消
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
//上传图片方法
- (void) imageUpload:(UIImage *) image{
//把图片转换成imageDate格式
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
//传送路径
NSString *urlString = @"http://*****/test/upload.php";
//建立请求对象
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
//设置请求路径
[request setURL:[NSURL URLWithString:urlString]];
//请求方式
[request setHTTPMethod:@"POST"];
//一连串上传头标签
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="vim_go.jpg"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
//上传文件开始
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//获得返回值
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
@end