菜鸟教程小白 发表于 2022-12-12 09:25:50

ios - 无法将图像加载到 ScrollView 中以进行缩放和滚动


                                            <p><p>我正在尝试将图像加载到 ScrollView 中并将手势识别器添加到图像中,以便可以放大和滚动。但不知何故,我无法让这个工作。我已经使用 xib 文件构建了应用程序。起初我将 ScrollView 放在 xib 文件中的 View 顶部,但因为它没有成功(给我一个空白的黑屏)然后我尝试以编程方式将 ScrollView 放在 .m 文件上。但现在它给了我一个空白的白色屏幕。我正在使用 SDWebImage 下载图像。这是我的代码:</p>

<pre><code>    #import &#34;ZoomAndPanViewController.h&#34;
#import &#34;UIImageView+WebCache.h&#34;

@interface ZoomAndPanViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImage *image;

- (void)centerScrollViewContents;
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer;

@end

@implementation ZoomAndPanViewController

@synthesize scrollView = _scrollView;

@synthesize imageView = _imageView;

- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width &lt; boundsSize.width) {
      contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
      contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height &lt; boundsSize.height) {
      contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
      contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // Get the location within the image view where we tapped
    CGPoint pointInView = ;

    // Get a zoom scale that&#39;s zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);

    // Figure out the rect we want to zoom to, then zoom to it
    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);

    ;
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
    ;
}


- (void)viewDidLoad
{
    ;
    // Do any additional setup after loading the view, typically from a nib.
    CGRect scrollFrame = [applicationFrame];
    self.scrollView = [initWithFrame:scrollFrame];
    self.scrollView.delegate = self;

    // Set a nice title
    self.title = @&#34;Image&#34;;
    self.imageView = [ init];
    self.image = [init];
    __block UIActivityIndicatorView *activityIndicator;
    placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
   {
         if (!activityIndicator)
         {
             NSLog(@&#34;setting up the indicator&#34;);
             ];
             activityIndicator.center = self.imageView.center;
             ;
             NSLog(@&#34;indicator start animating&#34;);
         }
   }
                        completed:^(UIImage *uiimage, NSError *error, SDImageCacheType cacheType)
   {
         NSLog(@&#34;download image completed&#34;);
         ;
         activityIndicator = nil;
         self.image = uiimage;
         ;
         NSLog(@&#34;image vs uiimage : %f x %f vs %f vs %f&#34;, self.image.size.width, self.image.size.height, uiimage.size.width, uiimage.size.height);
   }];

    // Set up the image we want to scroll &amp; zoom and add it to the scroll view


    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=self.image.size};
    ;

    // Tell the scroll view the size of the contents
    self.scrollView.contentSize = self.image.size;
    NSLog(@&#34;scrollview contentsize %f x %f&#34;,self.image.size.width,self.scrollView.contentSize.height);
    UITapGestureRecognizer *doubleTapRecognizer = [ initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    ;

    UITapGestureRecognizer *twoFingerTapRecognizer = [ initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    ;
    //;
    ;
    SDWebImageManager.sharedManager.imageDownloader.queueMode = SDWebImageDownloaderLIFOQueueMode;

}

- (void)viewDidUnload
{
    ;
    // Release any retained subviews of the main view.
}

- (void)viewWillAppear:(BOOL)animated {
    ;

    // Set up the minimum &amp; maximum zoom scales
    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;

    ;
}

- (void)didReceiveMemoryWarning
{
    ;
    // Dispose of any resources that can be recreated.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that we want to zoom
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // The scroll view has zoomed, so we need to re-center the contents
    ;
}

@end
</code></pre>

<p>任何关于我在代码中做错的线索以及任何帮助都将不胜感激。</p>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>只需创建 2 个名为 CustomScroll.h 和 .m 的类</p>

<p>在 CustomScroll.h 中写入</p>

<pre><code>@interface CustomScroll : UIScrollView
{

}

@property (nonatomic,strong) IBOutlet UIView *imageContainer;
</code></pre>

<p>在 .m 中编写以下代码:</p>

<pre><code>@synthesize imageContainer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

//self = ;
    if (self) {
      // Custom initialization
    }
    return self;

}

// Override layoutSubviews to center content

- (void)layoutSubviews
{

    ;

    // center the image as it becomes smaller than the size of the screen

    CGSize boundsSize = self.bounds.size;

    CGRect frameToCenter = imageContainer.frame;

    // center horizontally
    if (frameToCenter.size.width &lt; boundsSize.width)
      frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
      frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height &lt; boundsSize.height)
      frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
      frameToCenter.origin.y = 0;

    imageContainer.frame = frameToCenter;

    NSLog(@&#34;imageContainer.frame.height=== %f&#34;,imageContainer.frame.size.height);

}

-(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation
{

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}
</code></pre>

<p>并且在你想在 .h 文件中滚动图像的类中,导入 CustomScroll.h 并制作像 </p> 的对象

<pre><code>CustomScroll *customScrl;
</code></pre>

<p>并在您的 View 中获取 ScrollView 并将 imageview 放入其中并将 ScrollView 与 customScrl 绑定(bind)(不要忘记绑定(bind)委托(delegate)并在 .h 文件中添加 UIScrollViewDelegate)并在自定义类选项卡中将 CustomScroll 写为类。</p>

<p>现在在 .m 文件中写入要显示图像或在 viewDidLoad 中:</p>

<pre><code>// Zoom image inside Scrol view //
// outputImage is my imageview inside Scrollview
    self.customScrl.contentSize= CGSizeMake(outputImage.frame.size.width, outputImage.frame.size.height);
    self.customScrl.maximumZoomScale=5.0;
    self.customScrl.minimumZoomScale=1.0;
    self.customScrl.clipsToBounds=YES;
    self.customScrl.delegate=self;
    ;
    self.customScrl.imageContainer=outputImage;

    ;


#pragma ImageZooming Function

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIImageView *zoomImg = outputImage;
    //NSLog(@&#34;zoom img=== %@&#34;,zoomImg);

    return zoomImg;
}
</code></pre>

<p>照着写的去做。它肯定也对你有用。 :)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法将图像加载到 ScrollView 中以进行缩放和滚动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15239347/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15239347/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法将图像加载到 ScrollView 中以进行缩放和滚动