从UIWebView中调用iOS相册,并选择图片上传到Linux Web服务器

点击打开链接


======首先看以下ios端=======

ViewController.h


//

//  ViewController.h

//  Xcode_FileUpload

//

//  Created by KirSsu Ryu on 12-8-7.

//  Copyright (c) 2012 __JModule__. All rights reserved.

//


#import <UIKit/UIKit.h>


//UIWebViewDelegate 代理类:跟javascript相互传值。

//UINavigationControllerDelegate,UIImagePickerControllerDelegate 代理类:打开相册等一系列操作。

@interface ViewController : UIViewController<UIWebViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>{

    UIWebView *myWebView;

    UIImagePickerController *picker_library_;

}

//我的WebView控件

@property (nonatomic, retain) IBOutlet UIWebView *myWebView;

//相册类的变量

@property (nonatomic, retain) IBOutlet UIImagePickerController *picker_library_;


@end


ViewController.m

//

//  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 URLabsoluteString];  

    //根据":"拆分字符串,返回数组。

    NSArray *components = [requestString componentsSeparatedByString:@":"];

    //如果数组内的元素大于1并且第一个元素的值相等

    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0isEqualToString:@"gallery"]) {  

        if([(NSString *)[components objectAtIndex:1isEqualToString:@"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_

                            animatedYES];

}



//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


======ios端结束,再看下web端=======

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>测试</title>
<script type="text/javascript">
function uploads(){
sendCommand("open");
}  
function sendCommand(cmd){
var url = "gallery:"+cmd;
document.location = url;
}
</script>
</head>
<body>
<a href="#"><img src="upload.jpg" alt="上传按钮" οnclick="uploads();" /></a>
</body> 
</html> 

upload.php

<?
$uploaddir = $_SERVER["DOCUMENT_ROOT"].'/test/upload/'; //这个目录必须得有权限才行。
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)){
echo "OK";
}else{
echo "ERROR";
}
?>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值