【问题标题】:Insert UIImage into ScrollView in Code在代码中将 UIImage 插入到 ScrollView 中
【发布时间】:2013-10-18 14:27:02
【问题描述】:

我通过segue 传递一些数据(房间照片的名称、描述和名称)

我现在想将图像设置为scrollView,以便用户可以滚动全景图片。

我获得了正确的图片 URL,但我没有将图片放入 scrollView。

我的代码是这样的:

    #import "RoomsDetailVC.h"
    #import "RoomsViewController.h"

    @interface RoomsDetailVC ()

    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (strong, nonatomic) UIImageView *imageView;

    @end

    @implementation RoomsDetailVC

    @synthesize roomName;
    @synthesize priceLabel;
    @synthesize descriptionLabel;
    @synthesize roomImageString;

    - (void)viewDidLoad {
        [super viewDidLoad];

        self.roomName.title = [self.roomNameString description];
        self.priceLabel.text = [self.priceLabelString description];
        self.descriptionLabel.text = [self.descriptionLabelString description];

        [self.scrollView addSubview:_imageView];
        [self setImage];
    }

    - (void)setImage {
        if (self.scrollView) {
            NSString *imageName = [self.roomImageString description];
            NSURL *imageURL = [[NSBundle mainBundle] URLForResource:imageName withExtension:@"png"];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];


            UIImage *image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                self.scrollView.zoomScale = 1.0;
                self.scrollView.contentSize = image.size;
                self.imageView.image = image;
                self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
                NSLog(@"image: %@", imageURL);
            } else {
                NSLog(@"no image");
            }
        }
    }

    - (UIImageView *)imageView {
        if (!_imageView) _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        return _imageView;
        NSLog(@"imageView");
    }

    @end

self.scrollView.description 的 NSLog 结果是这样的:

scrollView: <UIScrollView: 0x1c114410; frame = (20 268; 280 160); clipsToBounds = YES; autoresize = RM+TM; gestureRecognizers = <NSArray: 0x1c114a10>; layer = <CALayer: 0x1c114660>; contentOffset: {0, 0}>

`self.imageView.description 的 NSLog 结果是这样的:

imageView: <UIImageView: 0x1c115e90; frame = (0 0; 516 160); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1c113160>>

【问题讨论】:

  • 你为什么要在字符串上调用description??

标签: iphone ios uiscrollview uiimageview xcode5


【解决方案1】:

我认为这是因为您在获取图像之前添加了子视图 (_imageView)。

反过来:

    [self.scrollView addSubview:_imageView];
    [self setImage];

    [self setImage];
    [self.scrollView addSubview:_imageView];

【讨论】:

    【解决方案2】:

    恐怕当您将_imageView 添加到滚动视图时,它实际上是nil。 您应该使用self.imageView,或者最好在下载后将imageView 添加到scrollView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2021-05-20
      • 2021-12-10
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多