2013-10-18 39 views
0

我通過segue(房間的照片的名稱,描述和名稱)通過一些數據插入的UIImage成滾動型的代碼

我現在想將圖像設置爲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>> 
+0

你爲什麼要在字符串上調用'description'? –

回答

2

我認爲這是因爲你正在添加子視圖(_imageView)甚至在獲取圖像之前。當您添加_imageView滾動視圖,它實際上nil

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

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

恐怕:

逆轉這種趨勢。 你應該使用self.imageView或者可能更好地將imageView加到scrollView下載後