ios - Camera view rotate 90 degree in Swift -
when i'm making customized camera app in swift. when try access camera, view in camera rotated 90 degrees. try find solution. 1 solution find adding fixorientation function fix view. not working... here full code:
let cihueadjust = "cihueadjust" let cihueadjustfilter = cifilter(name: "cihueadjust", withinputparameters: ["inputangle" : 1.24]) let filters = [cihueadjust: cihueadjustfilter] let filternames = [string](filters.keys).sort() class livecamviewcontroller : uiviewcontroller,avcapturevideodataoutputsamplebufferdelegate{ let maingroup = uistackview() let imageview = uiimageview(frame: cgrectzero) let filterscontrol = uisegmentedcontrol(items: filternames) override func viewdidload() { super.viewdidload() view.addsubview(maingroup) maingroup.axis = uilayoutconstraintaxis.vertical maingroup.distribution = uistackviewdistribution.fill maingroup.addarrangedsubview(imageview) maingroup.addarrangedsubview(filterscontrol) imageview.contentmode = uiviewcontentmode.scaleaspectfit filterscontrol.selectedsegmentindex = 0 let capturesession = avcapturesession() capturesession.sessionpreset = avcapturesessionpresetphoto let backcamera = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo) { let input = try avcapturedeviceinput(device: backcamera) capturesession.addinput(input) } catch { print("can't access camera") return } //get captureoutput invoked let previewlayer = avcapturevideopreviewlayer(session: capturesession) view.layer.addsublayer(previewlayer) let videooutput = avcapturevideodataoutput() videooutput.setsamplebufferdelegate(self, queue: dispatch_queue_create("sample buffer delegate", dispatch_queue_serial)) if capturesession.canaddoutput(videooutput) { capturesession.addoutput(videooutput) } capturesession.startrunning() } func captureoutput(captureoutput: avcaptureoutput!, didoutputsamplebuffer samplebuffer: cmsamplebuffer!, fromconnection connection: avcaptureconnection!) { guard let filter = filters[filternames[filterscontrol.selectedsegmentindex]] else { return } let pixelbuffer = cmsamplebuffergetimagebuffer(samplebuffer) let cameraimage = ciimage(cvpixelbuffer: pixelbuffer!) filter!.setvalue(cameraimage, forkey: kciinputimagekey) let filteredimage = uiimage(ciimage: filter!.valueforkey(kcioutputimagekey) as! ciimage!) let fixedimage = fixorientation(filteredimage) dispatch_async(dispatch_get_main_queue()) { self.imageview.image = fixedimage } } func fixorientation(image: uiimage) -> uiimage { if (image.imageorientation == uiimageorientation.up) { return image; } print(image.imageorientation) var transform = cgaffinetransformidentity switch (image.imageorientation) { case .down, .downmirrored: transform = cgaffinetransformtranslate(transform, image.size.width, image.size.height) transform = cgaffinetransformrotate(transform, cgfloat(m_pi)) break case .left, .leftmirrored: transform = cgaffinetransformtranslate(transform, image.size.width, 0) transform = cgaffinetransformrotate(transform, cgfloat(m_pi_2)) break case .right, .rightmirrored: transform = cgaffinetransformtranslate(transform, 0, image.size.height) transform = cgaffinetransformrotate(transform, cgfloat(-m_pi_2)) break case .up, .upmirrored: break } switch (image.imageorientation) { case .upmirrored, .downmirrored: transform = cgaffinetransformtranslate(transform, image.size.width, 0) transform = cgaffinetransformscale(transform, -1, 1) break case .leftmirrored, .rightmirrored: transform = cgaffinetransformtranslate(transform, image.size.height, 0) transform = cgaffinetransformscale(transform, -1, 1) break case .up, .down, .left, .right: break } //draw underlying cgimage new context, applying transform let ctx = cgbitmapcontextcreate(nil, int(image.size.width), int(image.size.height), cgimagegetbitspercomponent(image.cgimage), 0, cgimagegetcolorspace(image.cgimage), uint32(cgimagegetbitmapinfo(image.cgimage).rawvalue)) cgcontextconcatctm(ctx, transform); switch (image.imageorientation) { case .left, .leftmirrored, .right, .rightmirrored: cgcontextdrawimage(ctx, cgrectmake(0, 0, image.size.height, image.size.width), image.cgimage) break default: cgcontextdrawimage(ctx, cgrectmake(0, 0, image.size.width, image.size.height), image.cgimage) break } let cgimg = cgbitmapcontextcreateimage(ctx) let img = uiimage(cgimage:cgimg!) return img } override func viewdidlayoutsubviews() { maingroup.frame = cgrect(x: 37, y: 115, width: 301, height: 481) }
}
i set breakpoint test, code seems run until
if (image.imageorientation == uiimageorientation.up) { return image; }
then returns same view...
can me? thanks!!!
for need code @lovo in swift 3:
func rotatecameraimagetoproperorientation(imagesource : uiimage, maxresolution : cgfloat) -> uiimage { guard let imgref = imagesource.cgimage else { return imagesource } let width = cgfloat(imgref.width); let height = cgfloat(imgref.height); var bounds = cgrect(x: 0, y: 0, width: width, height: height) var scaleratio : cgfloat = 1 if (width > maxresolution || height > maxresolution) { scaleratio = min(maxresolution / bounds.size.width, maxresolution / bounds.size.height) bounds.size.height = bounds.size.height * scaleratio bounds.size.width = bounds.size.width * scaleratio } var transform = cgaffinetransform.identity let orient = imagesource.imageorientation let imagesize = cgsize(width: cgfloat(imgref.width), height: cgfloat(imgref.height)) switch(imagesource.imageorientation) { case .up : transform = cgaffinetransform.identity case .upmirrored : transform = cgaffinetransform(translationx: imagesize.width, y: 0.0) transform = transform.scaledby(x: -1.0, y: 1.0) case .down : transform = cgaffinetransform(translationx: imagesize.width, y: imagesize.height) transform = transform.rotated(by: cgfloat(m_pi)) case .downmirrored : transform = cgaffinetransform(translationx: 0.0, y: imagesize.height) transform = transform.scaledby(x: 1.0, y: -1.0) case .left : let storedheight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = storedheight transform = cgaffinetransform(translationx: 0.0, y: imagesize.width) transform = transform.rotated(by: 3.0 * cgfloat(m_pi) / 2.0) case .leftmirrored : let storedheight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = storedheight transform = cgaffinetransform(translationx: imagesize.height, y: imagesize.width) transform = transform.scaledby(x: -1.0, y: 1.0) transform = transform.rotated(by: 3.0 * cgfloat(m_pi) / 2.0) case .right : let storedheight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = storedheight transform = cgaffinetransform(translationx: imagesize.height, y: 0.0) transform = transform.rotated(by: cgfloat(m_pi) / 2.0) case .rightmirrored : let storedheight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = storedheight transform = cgaffinetransform(scalex: -1.0, y: 1.0) transform = transform.rotated(by: cgfloat(m_pi) / 2.0) } uigraphicsbeginimagecontext(bounds.size) guard let context = uigraphicsgetcurrentcontext() else { return imagesource } if orient == .right || orient == .left { context.scaleby(x: -scaleratio, y: scaleratio) context.translateby(x: -height, y: 0) } else { context.scaleby(x: scaleratio, y: -scaleratio) context.translateby(x: 0, y: -height) } context.concatenate(transform); context.draw(imgref, in: cgrect(x: 0, y: 0, width: width, height: height)) guard let imagecopy = uigraphicsgetimagefromcurrentimagecontext() else { return imagesource } uigraphicsendimagecontext(); return imagecopy; }
Comments
Post a Comment