ios - How to get URL from UIImage? -
i have ios app in there 2 ways user can picture:
select photos library (
uiimagepickercontroller)click custom made camera
here code clicking image custom camera (this within custom class called camera, subclass of uiview)
func clickpicture(completion:@escaping (uiimage) -> void) { guard let videoconnection = stillimageoutput?.connection(withmediatype: avmediatypevideo) else { return } videoconnection.videoorientation = .portrait stillimageoutput?.capturestillimageasynchronously(from: videoconnection, completionhandler: { (samplebuffer, error) -> void in guard let buffer = samplebuffer else { return } let imagedata = avcapturestillimageoutput.jpegstillimagensdatarepresentation(buffer) let dataprovider = cgdataprovider(data: imagedata! cfdata) let cgimageref = cgimage(jpegdataprovidersource: dataprovider!, decode: nil, shouldinterpolate: true, intent: .defaultintent) let image = uiimage(cgimage: cgimageref!, scale: 1, orientation: .right) completion(image) }) } here how click image within viewcontroller:
@ibaction func clickimage(_ sender: anyobject) { cameraview.clickpicture { (image) in //use "image" variable } } later, attempt upload picture user's icloud account using cloudkit. receive error saying record large. came across this post, says use ckasset. however, constructor ckasset requires url.
is there generic way can url uiimage? otherwise, how can url image clicked using custom camera (i have seen other posts getting url uiimagepickercontroller)? thanks!
ckasset represents external file (image, video, binary data , etc). why requires url init parameter.
in case recommend use following steps upload large image cloudkit:
- save
uiimagelocal storage (e.g. documents directory). - initialize
ckassetpath image in local storage. - upload asset cloud.
- delete image local storage when uploading completed.
here code:
// save image. let path = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true).first! let filepath = "\(path)/myimagename.jpg" uiimagejpegrepresentation(image, 1)!.writetofile(filepath, atomically: true) let asset = ckasset(fileurl: nsurl(fileurlwithpath: filepath)!) // upload asset here. // delete image. { try filemanager.default.removeitem(atpath: filepath) } catch { print(error) }
Comments
Post a Comment