Actually there is a code and if the picture is not loaded (is absent), when zooming in, SIGABRT is obtained:
Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]' How to be and what to do?
.h:
#import <UIKit/UIKit.h> @interface DetailViewController : UIViewController @property (strong, nonatomic) IBOutlet UIImage *img; @property (nonatomic, strong) IBOutlet UIScrollView *scrollView; @property NSString *downloadedimage; @end .m:
#import "DetailViewController.h" @interface DetailViewController () @property (nonatomic, strong) UIImageView *imageView; - (void)centerScrollViewContents; - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer; - (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer; @end @implementation DetailViewController @synthesize scrollView = _scrollView; @synthesize imageView = _imageView; - (void)centerScrollViewContents { CGSize boundsSize = self.scrollView.bounds.size; CGRect contentsFrame = self.imageView.frame; if (contentsFrame.size.width < boundsSize.width) { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f; } else { contentsFrame.origin.x = 0.0f; } if (contentsFrame.size.height < boundsSize.height) { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 5.0f; } else { contentsFrame.origin.y = 0.0f; } self.imageView.frame = contentsFrame; } - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer { CGPoint pointInView = [recognizer locationInView:self.imageView]; CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f; newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale); CGSize scrollViewSize = self.scrollView.bounds.size; CGFloat w = scrollViewSize.width / newZoomScale; CGFloat h = scrollViewSize.height / newZoomScale; CGFloat x = pointInView.x - (w / 2.0f); CGFloat y = pointInView.y - (h / 2.0f); CGRect rectToZoomTo = CGRectMake(x, y, w, h); [self.scrollView zoomToRect:rectToZoomTo animated:YES]; } - (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer { CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f; newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale); [self.scrollView setZoomScale:newZoomScale animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:_downloadedimage]; NSData *data = [NSData dataWithContentsOfURL:url]; self.img = [[UIImage alloc] initWithData:data]; self.imageView.image=_img; self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=_img.size}; [self.scrollView addSubview:self.imageView]; self.scrollView.contentSize = _img.size; UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)]; doubleTapRecognizer.numberOfTapsRequired = 2; doubleTapRecognizer.numberOfTouchesRequired = 1; [self.scrollView addGestureRecognizer:doubleTapRecognizer]; UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)]; twoFingerTapRecognizer.numberOfTapsRequired = 1; twoFingerTapRecognizer.numberOfTouchesRequired = 2; [self.scrollView addGestureRecognizer:twoFingerTapRecognizer]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHideNavbar:)]; [self.view addGestureRecognizer:tapGesture]; } -(void) showHideNavbar:(id) sender { if (self.navigationController.navigationBar.hidden == NO) { [self.navigationController setNavigationBarHidden:YES animated:YES]; } else if (self.navigationController.navigationBar.hidden == YES) { [self.navigationController setNavigationBarHidden:NO animated:YES]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"DetailView"; CGRect scrollViewFrame = self.scrollView.frame; CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width; CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height; CGFloat minScale = MIN(scaleWidth, scaleHeight); self.scrollView.minimumZoomScale = minScale; self.scrollView.maximumZoomScale = 1.0f; self.scrollView.zoomScale = minScale; [self centerScrollViewContents]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.imageView; } - (void)scrollViewDidZoom:(UIScrollView *)scrollView { [self centerScrollViewContents]; } @end