這是用swift寫的,,原理都一樣,,直接上代碼吧,也算是記錄一下折騰過的東西,,也提供一個(gè)參考吧,用到的時(shí)候自已優(yōu)化一下,,這只是個(gè)Demo。 這涉及到UIImagePickerController的使用以及網(wǎng)絡(luò)請(qǐng)求等,。 基本的步驟: 1,、彈出相冊(cè)(UIImagePickerController,實(shí)現(xiàn)這兩個(gè)代理 UIImagePickerControllerDelegate,UINavigationControllerDelegate ) 2,、選取圖片 3,、確認(rèn)選中并上傳(異步請(qǐng)求) 彈出相冊(cè) func showpic() { imagePicker.delegate=self imagePicker.sourceType=UIImagePickerControllerSourceType.PhotoLibrary imagePicker.modalTransitionStyle=UIModalTransitionStyle.CoverVertical imagePicker.allowsEditing=true self.presentViewController(imagePicker, animated:true, completion: nil) } UIImagePickerControllerDelegate 代理func imagePickerController(picker:UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject :AnyObject]) { self.dismissViewControllerAnimated(true, completion:nil); let gotImage=info[UIImagePickerControllerOriginalImage]as UIImage let midImage:UIImage=self.imageWithImageSimple(gotImage,scaledToSize:CGSizeMake(1000.0,1000.0))//這是對(duì)圖片進(jìn)行縮放,因?yàn)楣潭碎L(zhǎng)寬,,所以這個(gè)方法會(huì)變型,,有需要的自已去完善吧, 這里只是粗略使用,。 upload(midImage)//上傳 } //縮放圖片 func imageWithImageSimple(image:UIImage,scaledToSize newSize:CGSize)->UIImage { UIGraphicsBeginImageContext(newSize); image.drawInRect(CGRectMake(0,0,newSize.width,newSize.height)) let newImage:UIImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;
} func upload(img:UIImage) {
lb.frame=CGRectMake(0,0, self.view.bounds.size.width,20)
lb.textColor=UIColor.whiteColor() lb.text="上傳中...." lb.textAlignment=NSTextAlignment.Center lb.backgroundColor=UIColor.blackColor() lb.alpha=1
//添加風(fēng)火輪 av.frame=CGRectMake(200,200,20, 20) av.backgroundColor=UIColor.whiteColor() av.color=UIColor.redColor() av.startAnimating()
self.view.addSubview(av)
self.view.addSubview(lb)
let data=UIImagePNGRepresentation(img)//把圖片轉(zhuǎn)成data
let uploadurl:String="http://www./uploadpictest.aspx"//設(shè)置服務(wù)器接收地址 let request=NSMutableURLRequest(URL:NSURL(string:uploadurl)!)
request.HTTPMethod="POST"//設(shè)置請(qǐng)求方式 var boundary:String="-------------------21212222222222222222222" var contentType:String="multipart/form-data;boundary="+boundary request.addValue(contentType, forHTTPHeaderField:"Content-Type") var body=NSMutableData() body.appendData(NSString(format:"\r\n--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format:"Content-Disposition:form-data;name=\"userfile\";filename=\"dd.jpg\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format:"Content-Type:application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(data) body.appendData(NSString(format:"\r\n--\(boundary)").dataUsingEncoding(NSUTF8StringEncoding)!) request.HTTPBody=body let que=NSOperationQueue() NSURLConnection.sendAsynchronousRequest(request, queue: que, completionHandler: { (response, data, error) ->Void in
if (error != nil){ println(error) }else{ //Handle data in NSData type var tr:String=NSString(data:data,encoding:NSUTF8StringEncoding)! println(tr) //在主線程中更新UI風(fēng)火輪才停止 dispatch_sync(dispatch_get_main_queue(), { self.av.stopAnimating() //self.lb.hidden=true
})
} }) } 在服務(wù)器端接收,。我用的是C#,做個(gè)簡(jiǎn)的實(shí)現(xiàn),。
HttpPostedFile postfile=Request.Files[0];
postfile.SaveAs(Server.MapPath("test/0000.jpg");
Response.Write("上傳成功,!");
Response.End();
|
|