ios - AVCaptureSession rotation -


i'm trying app create uiimage correct way round.

most of code taken apple examples...

@interface cameramanager () <avcapturevideodataoutputsamplebufferdelegate>  @property (nonatomic, strong) cicontext *context; @property (nonatomic, strong) avcapturedevice *rearcamera;  @end  @implementation cameramanager  - (id)init {     if ((self = [super init])) {          self.context = [cicontext contextwithoptions:nil];         [self setupcamera];         [self addstillimageoutput];     }     return self; }  - (void)setupcamera {     self.session = [[avcapturesession alloc] init];     [self.session beginconfiguration];      [self.session setsessionpreset:avcapturesessionpresetphoto];      nsarray *devices = [avcapturedevice deviceswithmediatype:avmediatypevideo];     self.rearcamera = nil;     (avcapturedevice *device in devices) {         if (device.position == avcapturedevicepositionback) {             self.rearcamera = device;             break;         }     }      nserror *error = nil;     avcapturedeviceinput *input = [avcapturedeviceinput deviceinputwithdevice:self.rearcamera error:&error];     [self.session addinput:input];      avcapturevideodataoutput *dataoutput = [[avcapturevideodataoutput alloc] init];     [dataoutput setalwaysdiscardslatevideoframes:yes];      nsdictionary *options = @{(id)kcvpixelbufferpixelformattypekey : @(kcvpixelformattype_32bgra)};     [dataoutput setvideosettings:options];      [dataoutput setsamplebufferdelegate:self queue:dispatch_get_main_queue()];      [self.session addoutput:dataoutput];     [self.session commitconfiguration]; }  - (void)captureoutput:(avcaptureoutput *)captureoutput didoutputsamplebuffer:(cmsamplebufferref)samplebuffer fromconnection:(avcaptureconnection *)connection {     // grab pixel buffer     cvpixelbufferref pixelbuffer = (cvpixelbufferref) cmsamplebuffergetimagebuffer(samplebuffer);      // create ciimage it, rotate , 0 origin     ciimage *image = [ciimage imagewithcvpixelbuffer:pixelbuffer];     if ([[uiapplication sharedapplication] statusbarorientation] == uiinterfaceorientationlandscapeleft) {         image = [image imagebyapplyingtransform:cgaffinetransformmakerotation(m_pi)];     }     cgpoint origin = [image extent].origin;     image = [image imagebyapplyingtransform:cgaffinetransformmaketranslation(-origin.x, -origin.y)];      // set contents of uiimageview     cgimageref cgimage = [self.context createcgimage:image fromrect:[image extent]];     uiimage *uiimage = [uiimage imagewithcgimage:cgimage];      [[nsnotificationcenter defaultcenter] postnotificationname:@"image" object:uiimage];      cgimagerelease(cgimage); }  - (void)addstillimageoutput {     [self setstillimageoutput:[[avcapturestillimageoutput alloc] init]];     nsdictionary *outputsettings = [[nsdictionary alloc] initwithobjectsandkeys:avvideocodecjpeg,avvideocodeckey,nil];     [[self stillimageoutput] setoutputsettings:outputsettings];      avcaptureconnection *videoconnection = nil;     (avcaptureconnection *connection in [self.stillimageoutput connections]) {         (avcaptureinputport *port in [connection inputports]) {             if ([[port mediatype] isequal:avmediatypevideo] ) {                 videoconnection = connection;                 break;             }         }         if (videoconnection) {             break;         }     }      [[self session] addoutput:[self stillimageoutput]]; }  - (void)capturestillimage {     avcaptureconnection *videoconnection = nil;     (avcaptureconnection *connection in [[self stillimageoutput] connections]) {         (avcaptureinputport *port in [connection inputports]) {             if ([[port mediatype] isequal:avmediatypevideo]) {                 videoconnection = connection;                 break;             }         }         if (videoconnection) {             break;         }     }      nslog(@"about request capture from: %@", [self stillimageoutput]);     [[self stillimageoutput] capturestillimageasynchronouslyfromconnection:videoconnection                                                          completionhandler:^(cmsamplebufferref imagesamplebuffer, nserror *error) {                                                              cfdictionaryref exifattachments = cmgetattachment(imagesamplebuffer, kcgimagepropertyexifdictionary, null);                                                              if (exifattachments) {                                                                  nslog(@"attachements: %@", exifattachments);                                                              } else {                                                                  nslog(@"no attachments");                                                              }                                                              nsdata *imagedata = [avcapturestillimageoutput jpegstillimagensdatarepresentation:imagesamplebuffer];                                                              uiimage *image = [[uiimage alloc] initwithdata:imagedata];                                                               [[nsnotificationcenter defaultcenter] postnotificationname:kimagecapturedsuccessfully object:image];                                                          }]; } 

this camera manager class code.

i displaying preview of camera using outputsamplebufferdelegate (for various reasons).

i'm using session output "take photo".

the method capturestillimage bit i'm trying fix.

the photos taken device in landscapeleft orientation (the interface landscapeleft).

the previews show correct way around , exif data shows width , height correct way around too. (x = 3264, y = 2448).

but when display uiimage rotated 90 degrees counter clockwise. aspect ratio of image correct (i.e. looks fine, circles still circles) rotation.

i have found several categories claim fix this.

i have found several stackoverflow questions answers claim fix it.

none of these worked.

does know how rotate thing right way around?

adding following code before call capturestillimageasynchronouslyfromconnection usually:

if ([videoconnection isvideoorientationsupported]) {     [videoconnection setvideoorientation:[uidevice currentdevice].orientation]; } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -