ios - UIView with AVCaptureVideoPreviewLayer doesn't resize -


i need have custom camera bottom layer camera, , layer on top of png picture hole in see part of camera through hole. both layers should in size of png photo, fit size of screen, , maintain png's original aspect ratio.

in storyboard have uiview present camera, , uiimageview present png on top of it, both auto-layout superview.

i've managed png right wanted using code (works fine):

uiimage *passedimage = [uiimage imagenamed:self.phototitle]; cgrect imagebounds = cgrectmake(0, 0, passedimage.size.width, passedimage.size.height); self.imageview.frame = imagebounds; [self.imageview setimage:passedimage]; self.imageview.contentmode = uiviewcontentmodescaleaspectfit; 

i got camera displaying in uiview using avfoundation (avcapturevideopreviewlayer) code:

avcapturevideopreviewlayer *previewlayer = [[avcapturevideopreviewlayer alloc] initwithsession:session]; [previewlayer setvideogravity:avlayervideogravityresizeaspectfill]; calayer *rootlayer = [self.camerafeedview layer]; [rootlayer setmaskstobounds:yes]; cgrect frame = self.camerafeedview.frame; [previewlayer setframe:frame]; previewlayer.frame = rootlayer.bounds; [rootlayer insertsublayer:previewlayer atindex:0]; 

this way have png in center of screen wanted camera behind expands bounds of screen while need camera view same size image , behind it.

i've tried resize uiview same way resized uiimageview instead of shrinking view fit screen gets png's original size (larger screen) , flows out of screen.

i'm trying solve problem long time haven't found answer yet. i'm pretty new objective-c programming. please me, i'm desperate.

i've solution problem.

you want show avcapturevideo on exact area image bounds, behind image. think trying prepare framing functionality on video.

here solution. please call code after setting image uiimage. require imagescale method i've mentioned following code.

here, previewlayer size set according image fits uiimageview. make sure set image aspectfit requires.

//get ratio of uiimage size on uiimageview. cgsize sizemade = [self imagescale];  //change uiimageview frame according image size. float fheight = passedimage.image.size.height*sizemade.height; float fwidth = passedimage.image.size.width*sizemade.width;  cgrect tframe = previewlayer.frame; tframe.size.height = fheight; tframe.size.width = fwidth; previewlayer.frame = tframe; 

function ratio of image scale uiimageview.

- (cgsize)imagescale{      cgfloat sx = _imageviewabouttozoom.frame.size.width / _imageabouttozoom.size.width;     cgfloat sy = _imageviewabouttozoom.frame.size.height / _imageabouttozoom.size.height;     cgfloat s = 1.0;     switch (_imageviewabouttozoom.contentmode) {         case uiviewcontentmodescaleaspectfit:             s = fminf(sx, sy);             return cgsizemake(s, s);             break;          case uiviewcontentmodescaleaspectfill:             s = fmaxf(sx, sy);             return cgsizemake(s, s);             break;          case uiviewcontentmodescaletofill:             return cgsizemake(sx, sy);          default:             return cgsizemake(s, s);     } } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -