swift - Cropping a captured still image -
i want specific part of captured image cropped in circular shape right before it's viewed.
here code capturing still images:
@ibaction func takephoto(sender: uibutton) { self.stillimageoutput!.capturestillimageasynchronouslyfromconnection(self.stillimageoutput!.connectionwithmediatype(avmediatypevideo)) { (buffer:cmsamplebuffer!, error:nserror!) -> void in var image = avcapturestillimageoutput.jpegstillimagensdatarepresentation(buffer) var data_image = uiimage(data: image) self.previewimage.image = data_image } }
thank you
if let imagedata = avcapturestillimageoutput.jpegstillimagensdatarepresentation(buffer) { if let image = uiimage(data: imagedata) { let square = image.size.width < image.size.height ? cgsize(width: image.size.width, height: image.size.width) : cgsize(width: image.size.height, height: image.size.height) let imageview = uiimageview(frame: cgrect(origin: cgpoint(x: 0, y: 0), size: square)) imageview.contentmode = uiviewcontentmode.scaleaspectfill imageview.image = image imageview.layer.cornerradius = square.width/2 imageview.layer.maskstobounds = true uigraphicsbeginimagecontext(imageview.bounds.size) imageview.layer.renderincontext(uigraphicsgetcurrentcontext()) let result = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() self.previewimage.image = result } }
Comments
Post a Comment