Haven't written a blog for a long time, today specially wrote this, is also one side to imitate others to write the demo, the annotation part is Needed. Please refer to the students who need it.
@IBAction func headimagebtnpage () {//upload Avatar
Let Actionsheet = Uialertcontroller (title: "upload avatar", message:nil, preferredstyle:. Actionsheet)
Let cancelbtn = uialertaction (title: "cancel", style:.) Cancel, Handler:nil)
//
//
Let Takephotos = uialertaction (title: "take pictures", style:.) destructive, Handler: {
(action:uialertaction), Void in
Determine if you can take a photo, open the camera if you can
If Uiimagepickercontroller.issourcetypeavailable (. Camera) {
Let picker = Uiimagepickercontroller ()
Picker.sourcetype =. Camera
Picker.delegate = Self
Picker.allowsediting = True
Self.presentviewcontroller (picker, animated:true, Completion:nil)
//
// }
Else
// {
Print ("simulate The camera cannot be turned on, please use in the real machine");
// }
//
// })
Let Selectphotos = uialertaction (title: "album selection", style:.) Default, Handler: {
(action:uialertaction)
-Void in
Call album feature to open album
Let picker = Uiimagepickercontroller ()
Picker.sourcetype =. Photolibrary
Picker.delegate = Self
Picker.allowsediting = True
Self.presentviewcontroller (picker, animated:true, Completion:nil)
//
// })
Actionsheet.addaction (cancelbtn)
Actionsheet.addaction (TAKEPHOTOS)
Actionsheet.addaction (SELECTPHOTOS)
Self.presentviewcontroller (actionsheet, animated:true, Completion:nil)
//
}
}
Mark:imagepickercontroller Agent
Func imagepickercontroller (picker:uiimagepickercontroller, didfinishpickingmediawithinfo info: [string:anyobject]) {
Let type:string = (info[uiimagepickercontrollermediatype] as! String)
When the selection type is a picture
if type = = "head_icon_demo" {
Fix the position of the picture
Let image = Fixorientation ((info[uiimagepickercontrolleroriginalimage] as! UIImage))
Turn the picture into NSData first
Let data = UIImageJPEGRepresentation (image, 0.5)
Path to Picture saving
Place the picture in the Documents folder of the sandbox
Home Directory
Let homedirectory = Nshomedirectory ()
Let documentpath = homedirectory + "/documents"
File Manager
Let Filemanager:nsfilemanager = Nsfilemanager.defaultmanager ()
Copy the data object that you just converted into a sandbox and save it as Image.png
do {
Try Filemanager.createdirectoryatpath (documentpath, withintermediatedirectories:true, Attributes:nil)
}
Catch _ {
}
Filemanager.createfileatpath (documentpath.stringbyappendingstring ("/image.png"), contents:data, Attributes:nil)
Get the full path of the picture in the sandbox after the selection
Let filepath:string = String (format: "%@%@", documentpath, "/image.png")
Print ("filePath:" + FilePath)
Send request to upload Avatar network request
Alamofire.upload (. POST, "http://192.168.3.16:9060/client/updateHeadUrl", Multipartformdata: {multipartformdata in
Let Lastdata = NSData (contentsoffile:filepath)
//
Multipartformdata.appendbodypart (data:lastdata!, name: "image")
//
}, encodingcompletion: {response in
picker.dismissviewcontrolleranimated (true, Completion:nil)
Switch response {
Case. Success (let upload, _, _):
Upload.responsejson (completionhandler: {(response) in
Print (response)
Self.imageView.image = UIImage (data:data!)
//
// })
//
Case. Failure (let encodingerror):
Print (encodingerror)
// }
// })
}
}
Func fixorientation (aimage:uiimage)-UIImage {//handling avatar Picture Angle Issues
No-op If the orientation is already correct
If aimage.imageorientation = =. Up {
Return Aimage
}
We need to calculate the proper transformation to make the image upright.
We do it on 2 steps:rotate if left/right/down, and then flip if Mirrored.
var transform:cgaffinetransform = cgaffinetransformidentity
Switch Aimage.imageorientation {
Case. Down,. Downmirrored:
Transform = cgaffinetransformtranslate (transform, aImage.size.width, aImage.size.height)
Transform = cgaffinetransformrotate (transform, cgfloat (m_pi))
Case. Left,. Leftmirrored:
Transform = cgaffinetransformtranslate (transform, aImage.size.width, 0)
Transform = cgaffinetransformrotate (transform, cgfloat (m_pi_2))
Case. Right,. Rightmirrored:
Transform = cgaffinetransformtranslate (transform, 0, aImage.size.height)
Transform = cgaffinetransformrotate (transform, cgfloat (-m_pi_2))
Default
Break
}
Switch Aimage.imageorientation {
Case. Upmirrored,. Downmirrored:
Transform = cgaffinetransformtranslate (transform, aImage.size.width, 0)
Transform = Cgaffinetransformscale (transform,-1, 1)
Case. Leftmirrored,. Rightmirrored:
Transform = cgaffinetransformtranslate (transform, aImage.size.height, 0)
Transform = Cgaffinetransformscale (transform,-1, 1)
Default
Break
}
Now we draw the underlying cgimage into a new context, applying the transform
Calculated Above.
Here you need to pay attention to the next cgimagegetbitmapinfo, its type is Int32, cgimagegetbitmapinfo (aimage.cgimage). rawValue, It's not an error to Write.
Let ctx:cgcontextref = cgbitmapcontextcreate (nil, int (aImage.size.width), int (aImage.size.height), Cgimagegetbitspercomponent (aimage.cgimage), 0, cgimagegetcolorspace (aimage.cgimage), cgimagegetbitmapinfo ( Aimage.cgimage). rawValue)!
CGCONTEXTCONCATCTM (ctx, Transform)
Switch Aimage.imageorientation {
Case. Left,. Leftmirrored,. Right,. Rightmirrored:
Grr ...
Cgcontextdrawimage (ctx, cgrectmake (0, 0, aImage.size.height, aImage.size.width), Aimage.cgimage)
Default
Cgcontextdrawimage (ctx, cgrectmake (0, 0, aImage.size.width, aImage.size.height), Aimage.cgimage)
}
And now we just create a new UIImage from the drawing context
Let Cgimg:cgimageref = Cgbitmapcontextcreateimage (ctx)!
Let Img:uiimage = UIImage (cgimage:cgimg)
return img
}
Swift Upload Avatar