@@ -252,16 +252,19 @@ - (void)hitTestSceneObjects:(const CGPoint)tapPoint resolve:(RCTARKitResolve)res
252252}
253253
254254
255- - (UIImage *)getSnaphshot {
255+ - (UIImage *)getSnapshot : ( NSDictionary *) selection {
256256 UIImage *image = [self .arView snapshot ];
257- return image;
257+
258+
259+ return [self cropImage: image toSelection: selection];
260+
258261}
259262
260263
261264
262265
263266
264- - (UIImage *)getSnaphsotCamera {
267+ - (UIImage *)getSnapshotCamera : ( NSDictionary *) selection {
265268 CVPixelBufferRef pixelBuffer = self.arView .session .currentFrame .capturedImage ;
266269 CIImage *ciImage = [CIImage imageWithCVPixelBuffer: pixelBuffer];
267270
@@ -274,11 +277,102 @@ - (UIImage *)getSnaphsotCamera {
274277
275278 UIImage *image = [UIImage imageWithCGImage: videoImage scale: 1.0 orientation: UIImageOrientationRight];
276279 CGImageRelease (videoImage);
277- return image;
280+
281+ UIImage *cropped = [self cropImage: image toSelection: selection];
282+ return cropped;
283+
278284}
279285
280286
281287
288+ - (UIImage *)cropImage : (UIImage *)imageToCrop toRect : (CGRect)rect
289+ {
290+ // CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
291+
292+ CGImageRef imageRef = CGImageCreateWithImageInRect ([imageToCrop CGImage ], rect);
293+ UIImage *cropped = [UIImage imageWithCGImage: imageRef];
294+ CGImageRelease (imageRef);
295+
296+ return cropped;
297+ }
298+
299+ static inline double radians (double degrees) {return degrees * M_PI/180 ;}
300+ UIImage* rotate (UIImage* src, UIImageOrientation orientation)
301+ {
302+ UIGraphicsBeginImageContext (src.size );
303+
304+ CGContextRef context = UIGraphicsGetCurrentContext ();
305+ [src drawAtPoint: CGPointMake (0 , 0 )];
306+ if (orientation == UIImageOrientationRight) {
307+ CGContextRotateCTM (context, radians (90 ));
308+ } else if (orientation == UIImageOrientationLeft) {
309+ CGContextRotateCTM (context, radians (-90 ));
310+ } else if (orientation == UIImageOrientationDown) {
311+ // NOTHING
312+ } else if (orientation == UIImageOrientationUp) {
313+ CGContextRotateCTM (context, radians (90 ));
314+ }
315+
316+
317+
318+ UIImage *image = UIGraphicsGetImageFromCurrentImageContext ();
319+ UIGraphicsEndImageContext ();
320+ return image;
321+ }
322+ - (UIImage *)cropImage : (UIImage *)imageToCrop toSelection : (NSDictionary *)selection
323+ {
324+
325+ // selection is in view-coordinate system
326+ // where as the image is a camera picture with arbitary size
327+ // also, the camera picture is cut of so that it "covers" the self.bounds
328+ // if selection is nil, crop to the viewport
329+
330+ UIImage * image = rotate (imageToCrop, imageToCrop.imageOrientation );
331+
332+ float arViewWidth = self.bounds .size .width ;
333+ float arViewHeight = self.bounds .size .height ;
334+ float imageWidth = image.size .width ;
335+ float imageHeight = image.size .height ;
336+
337+ float arViewRatio = arViewHeight/arViewWidth;
338+ float imageRatio = imageHeight/imageWidth;
339+ float imageToArWidth = imageWidth/arViewWidth;
340+ float imageToArHeight = imageHeight/arViewHeight;
341+
342+ float finalHeight;
343+ float finalWidth;
344+
345+
346+ if (arViewRatio > imageRatio)
347+ {
348+ finalHeight = arViewHeight*imageToArHeight;
349+ finalWidth = arViewHeight*imageToArHeight /arViewRatio;
350+ }
351+ else
352+ {
353+ finalWidth = arViewWidth*imageToArWidth;
354+ finalHeight = arViewWidth * imageToArWidth * arViewRatio;
355+ }
356+
357+ float topOffset = (image.size .height - finalHeight)/2 ;
358+ float leftOffset = (image.size .width - finalWidth)/2 ;
359+
360+
361+ float x = leftOffset;
362+ float y = topOffset;
363+ float width = finalWidth;
364+ float height = finalHeight;
365+ if (selection && selection != [NSNull null ]) {
366+ x = leftOffset+ [selection[@" x" ] floatValue ]*imageToArWidth;
367+ y = topOffset+[selection[@" y" ] floatValue ]*imageToArHeight;
368+ width = [selection[@" width" ] floatValue ]*imageToArWidth;
369+ height = [selection[@" height" ] floatValue ]*imageToArHeight;
370+ }
371+ CGRect rect = CGRectMake (x, y, width, height);
372+
373+ UIImage *cropped = [self cropImage: image toRect: rect];
374+ return cropped;
375+ }
282376
283377
284378#pragma mark - plane hit detection
@@ -408,7 +502,7 @@ - (void)renderer:(id <SCNSceneRenderer>)renderer willUpdateNode:(SCNNode *)node
408502
409503- (void )renderer : (id <SCNSceneRenderer>)renderer didUpdateNode : (SCNNode *)node forAnchor : (ARAnchor *)anchor {
410504 ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
411-
505+
412506 if (self.onPlaneUpdate ) {
413507 self.onPlaneUpdate (@{
414508 @" id" : planeAnchor.identifier .UUIDString ,
0 commit comments