Swift 2.0 image upload and image client compression is to write a certain app, pull away, in fact, relatively simple, and did not do a lot of iOS internal mechanism to judge, just to achieve the image upload and image compression upload after the function.
//
Viewcontroller.swift
Uploadimage
//
Created by Jincon on 15/11/1.
COPYRIGHT©2015 year Jincon. All rights reserved.
//
Import Uikit
Class Viewcontroller:uiviewcontroller, Uiimagepickercontrollerdelegate,
uinavigationcontrollerdelegate {
@IBOutlet weak var imageview:uiimageview!
@IBAction func fromalbum (sender:anyobject) {
Determine if the settings support picture libraries
If Uiimagepickercontroller.issourcetypeavailable (. Photolibrary) {
Initializing the picture controller
Let picker = Uiimagepickercontroller ()
Set up agents
Picker.delegate = Self
Specify picture Controller type
Picker.sourcetype = Uiimagepickercontrollersourcetype.photolibrary
Set whether editing is allowed
picker.allowsediting = Editswitch.on
Eject controller, display interface
Self.presentviewcontroller (picker, animated:true, completion: {
()-> Void in
})
}else{
Print ("read album error")
}
}
Select picture after successful agent
Func Imagepickercontroller (Picker:uiimagepickercontroller,
Didfinishpickingmediawithinfo info: [String:anyobject]) {
View Info Object
Print (info)
Get the selected artwork
Let image = Info[uiimagepickercontrolleroriginalimage] as! UIImage
Imageview.image = Image
Let Midimage:uiimage=self.imagewithimagesimple (Image,scaledtosize:cgsizemake (800.0,800.0))
Upload (midimage)
Picture Controller exit
Picker.dismissviewcontrolleranimated (True, completion: {
()-> Void in
})
}
Picture and other proportional compression
Func imagewithimagesimple (image:uiimage,scaledtosize newsize:cgsize)->uiimage
{
var width:cgfloat!
var height:cgfloat!
Equal proportional scaling
If Image.size.width/newsize.width >= image.size.height/newsize.height{
width = newsize.width
Height = image.size.height/(image.size.width/newsize.width)
}else{
Height = newsize.height
width = image.size.width/(image.size.height/newsize.height)
}
Let Sizeimagesmall = cgsizemake (width, height)
End
Print (Sizeimagesmall)
Uigraphicsbeginimagecontext (Sizeimagesmall);
Image.drawinrect (CGRectMake (0,0,sizeimagesmall.width,sizeimagesmall.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= "Upload ..."
Lb.textalignment=nstextalignment.center
Lb.backgroundcolor=uicolor.blackcolor ()
Lb.alpha=1
Add Hot Wheels
Let av = Uiactivityindicatorview ()
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)//Turn the picture into data
Let uploadurl:string= "http://xxxx/upload.php"//Set server receive address
Let Request=nsmutableurlrequest (Url:nsurl (string:uploadurl)!)
Request. Httpmethod= "POST"//Set Request mode
Let boundary:string= "-------------------21212222222222222222222"
Let contenttype:string= "multipart/form-data;boundary=" +boundary
Request.addvalue (ContentType, Forhttpheaderfield: "Content-type")
Let Body=nsmutabledata ()
Body.appenddata (format: \r\n--\ (boundary) \ r \ n) nsstring datausingencoding (nsutf8stringencoding)!)
Body.appenddata (format: "content-disposition:form-data;name=\" userfile\ "filename=\" dd.jpg\ "\ r \ n"). Datausingencoding (nsutf8stringencoding)!
Body.appenddata (format: "content-type:application/octet-stream\r\n\r\n"). Datausingencoding (NSString 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) {
Print (Error)
}else{
Handle data in NSData type
Let tr:string=nsstring (data:data!,encoding:nsutf8stringencoding)! As String
Print (TR)
Update UI in main thread the steamer just stopped.
Dispatch_sync (Dispatch_get_main_queue (), {
Av.stopanimating ()
Self.lb.hidden=true
})
}
})
}
Override Func Viewdidload () {
Super.viewdidload ()
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
Server-side code: I write simple:
<?php
Move_uploaded_file ($_files["UserFile"] ["Tmp_name"], "1.jpg");
echo "1";
&NBSP
?