2013-03-18 105 views
1

我想開發一個庫,它需要一個圖像數組,並返回一個scrollView與圖像並排。UIImageView沒有添加到UIView

但是,當我添加此scrollView,我回到我的主視圖,它不會添加圖像。圖像是正確的。

我的想法是並排使用每個圖像的滾動視圖,並使用滾動條來顯示幻燈片。首先,這個概念好嗎?

其次,我不明白爲什麼我在模擬器中運行應用程序時不顯示圖像。

需要更多的細節請詢問。

代碼如下。 我的頭文件:

#import <UIKit/UIKit.h> 

@interface HCIImageSlideShowView : UIScrollView 

-(void) setImages:(NSArray*) imagesArray; 
-(void) setBounds:(CGRect)bounds; 
-(void) setCaptions:(NSArray*) imageCaptionsArray; 

-(void) isEditable:(BOOL)edit; 

-(void) setSlideTime:(int) milliSeconds; 

-(void) startSlideShow; 

- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray 
       bounds:(CGRect)bounds slideTime:(int)milliSeconds; 

@end 

我實現文件:

#import "HCIImageSlideShowView.h" 
#import "HCIResultListViewController.h" 

@interface HCIImageSlideShowView() 

@property (strong,nonatomic) NSMutableArray *imagesArray; 
@property BOOL editable; 
@property (nonatomic) int slideTime; 
@property (strong,nonatomic) NSMutableArray *imageCaptionsArray; 
@property CGFloat width; 

@end 


@implementation HCIImageSlideShowView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray 
       bounds:(CGRect)bounds slideTime:(int)milliSeconds 
{ 

    NSLog([NSString stringWithFormat:@"%f,%f", bounds.size.width , bounds.size.height]); 

    CGFloat width_t = bounds.size.width; 

    bounds.size.width = [imagesArray count] * bounds.size.width; 

    self = [[HCIImageSlideShowView alloc] initWithFrame:bounds]; 

    _width = width_t; 

    [self setBackgroundColor:[[UIColor alloc] initWithRed:0.2 green:0.1 blue:0.3 alpha:0.4]]; 

    if (self) { 
     [self setImages:imagesArray]; 
     [self setSlideTime:milliSeconds]; 
     [self setCaptions:captionArray]; 
     [self defaultLoad]; 
    } 

    self.scrollEnabled = YES; 

    return self; 
} 

-(void) defaultLoad 
{ 

    NSLog([NSString stringWithFormat:@"%f,%f,%f",_width,self.bounds.size.height,self.bounds.size.width]); 

    for (int i = 0; i < [_imagesArray count]; i++) { 
     CGRect imageBounds = CGRectMake(i * _width, self.bounds.size.height, _width, self.bounds.size.height); 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageBounds]; 
     [imageView setImage:[HCIResultListViewController resizeImage:_imagesArray[i] withWidth:_width withHeight:self.bounds.size.height]]; 
     NSLog([NSString stringWithFormat:@"%f,%f",imageView.bounds.size.height,imageView.bounds.size.width]); 
     [self addSubview:imageView]; 
    } 

} 

-(void) setBounds:(CGRect)bounds 
{ 
    self.bounds = bounds; 
} 

-(void) setImages:(NSArray *)imagesArray 
{ 
    _imagesArray = [[NSMutableArray alloc] initWithArray:imagesArray]; 
} 

-(void) setSlideTime:(int)milliSeconds 
{ 
    _slideTime = milliSeconds; 
} 

-(void) startSlideShow 
{ 

} 

-(void) isEditable:(BOOL)edit 
{ 
    _editable = edit; 
} 

-(void) setCaptions:(NSArray *)imageCaptionsArray { 
    _imageCaptionsArray = [[NSMutableArray alloc] initWithArray:imageCaptionsArray]; 
} 

@end 
+0

檢查你的自我是否得到建立與否,如果(個體經營)條件語句旁邊寫着setBackgroundColor – Mrunal 2013-03-18 13:40:30

+0

爲什麼你的init方法中分配一個新HCIImageSlideShowView對象? AFAIK只需調用'self = [super initWithFrame:...];應該就足夠了''根據你對'self'返回的內容以及你是否使用IB,這可能是好的。但是你冒險最終得到兩個HCIImageSlideShowView,一個設置正確但沒有顯示,另一個顯示爲空。 – 2013-03-18 13:42:22

+0

是的。自我正在創造。在屏幕上它實際上顯示背景顏色。但是,圖像不顯示。滾動條都沒有。 – 2013-03-18 13:42:34

回答

2

似乎有很多你的代碼錯誤 - 尤其是你的初始化器。下面是一個註釋版本

@interface HCIImageSlideShowView : UIScrollView 


-(void) startSlideShow; 

- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray 
       bounds:(CGRect)bounds slideTime:(int)milliSeconds; 
    /* 
    remove this custom initialiser as it is quite wrong. 
    Use the default initialiser (don't override) 
    then set the properties after you have created the object. 
    */ 


    /* Remove all of your custom setters. 
    If any of these properties need setting outside of the class, 
    move the property declarations to the .h file. 
    */ 

    -(void) setImages:(NSArray*) imagesArray; 
    -(void) setBounds:(CGRect)bounds; 
    -(void) setCaptions:(NSArray*) imageCaptionsArray; 

    -(void) isEditable:(BOOL)edit; 

    -(void) setSlideTime:(int) milliSeconds; 

@end 



    #import "HCIImageSlideShowView.h" 
    #import "HCIResultListViewController.h" 

@interface HCIImageSlideShowView() 
/* 
make these properties public by moving them to your .h file 
so that you can set them from the calling object 
*/ 
@property (strong,nonatomic) NSMutableArray *imagesArray; 
@property BOOL editable; 
@property (nonatomic) int slideTime; 
@property (strong,nonatomic) NSMutableArray *imageCaptionsArray; 
@property CGFloat width; 
@end 

@implementation HCIImageSlideShowView 

- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray 
       bounds:(CGRect)bounds slideTime:(int)milliSeconds 
/* 
    You seem to be getting confused with init, 
    so I suggest you do not make a custom init method at all. 
    Initialise your object with the default initWithFrame, 
    then have the caller set properties on your newly 
     made object after initiliasation. 

*/ 

{ 
    NSLog([NSString stringWithFormat:@"%f,%f", bounds.size.width , bounds.size.height]); 
    /* 
     This is the way to use NSLog... 
     NSLog(@"%f,%f", bounds.size.width , bounds.size.height); 
    */ 

    CGFloat width_t = bounds.size.width; 
    /* 
    double assignment: two lines further down you assign 
    width_t to _width. You can do that here in one step 
    */ 

    bounds.size.width = [imagesArray count] * bounds.size.width; 

    self = [[HCIImageSlideShowView alloc] initWithFrame:bounds]; 

    /* 
    This is wrong. Although as Hermann says it may be _legal_, don't do it! 
     -Never alloc an object inside it's own initialiser. 
     The memory will already have been alloc'd by the caller. 
     - Never assign to self anything but the return value from super's init. 
    */ 

    _width = width_t; 

    /* 
    please be consistent with your iVar/property naming. 
    Here you are addressing the ivar _width, 2 lines up 
    you are using the property accessor syntax self.bounds. 
    In you case I would recommend ALWAYS using self.varName 
    except inside a custom setter or getter. 
    */ 

    [self setBackgroundColor:[[UIColor alloc] initWithRed:0.2 green:0.1 blue:0.3 alpha:0.4]]; 

    if (self) { 
      //[self setImages:imagesArray]; 
     self.imagesArray = [imagesArray mutableCopy]; 

      //[self setSlideTime:milliSeconds]; 
     self.slideTime = milliSeconds; 

      //[self setCaptions:captionArray]; 
     self.imageCaptionsArray = [captionArray mutableCopy]; 

     /* 
     As my comment above - use property syntax and try to 
     avoid writing your own setters and getters 
     unless you have a very good reason. 
     */ 

     [self defaultLoad]; 
    } 

    self.scrollEnabled = YES; 

    return self; 
} 

當你的初始化代碼需要一些清理,我也沒太仔細地看defaultLoad - 但是我確實有一對夫婦的意見...

(1)

CGRect imageBounds = CGRectMake(i * _width, self.bounds.size.height, _width, self.bounds.size.height); 

應該

CGRect imageBounds = CGRectMake(i * _width,0, _width, self.bounds.size.height); 

否則你的圖片都是PL在您的scrollView的高度下方脫離屏幕。

(2)

您需要設置一個contentSize使滾動視圖可以滾動

[scrollView setContentSize:(CGSize){self.width*[imageArray count],self.bounds.size.height}]; 

其他更普遍的評論是,這是一些圖片OK,但你不會想要製作一個帶有大量屏幕imageViews的scrollView,因爲你會吃掉內存。您實際上只需要三個,即屏幕上當前的圖像,以及左側和右側的前一個和下一個圖像。這個想法是隻在你可能需要它們的時候加載你的圖像,並且像tableView那樣回收你的imageViews。

你應該與它的伴隨WWDC 2010/11視頻(S)和slides一起看看Apple's Photoscroller sample app

不要擔心所有的平鋪和縮放的細節,這是很好的得到的一般原則將對象創建保持在最低限度,並在可行的情況下回收/再利用對象。順便說一句,你可能根本不需要自定義的scrollView對象:你可以在調用viewController對象的幾行代碼中實現大部分的功能。例如...

- (void) viewDidLoad { 
     [super viewDidLoad]; 
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
    [scrollView setBackgroundColor:[UIColor redColor]]; 
    NSArray* imageArray = @[[UIImage imageNamed:@"image1.png"] 
          ,[UIImage imageNamed:@"image2.png"] 
          ,[UIImage imageNamed:@"image3.png"] 
          ]; 
    CGFloat width = scrollView.bounds.size.width; 
    CGFloat height = scrollView.bounds.size.height; 

    for (int i = 0; i < [imageArray count]; i++) { 
     CGRect imageFrame = CGRectMake(i * width, 0, width, height); 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame]; 
     [imageView setImage:imageArray[i]]; 
     [scrollView addSubview:imageView]; 
    } 
    [scrollView setContentSize:(CGSize){width*[imageArray count],height}]; 
    [self.view addSubview:scrollView]; 
} 
0
- (id) initWithImages:(NSArray*)imagesArray captionsArray:(NSArray*) captionArray 
       bounds:(CGRect)bounds slideTime:(int)milliSeconds 
{ 

    NSLog([NSString stringWithFormat:@"%f,%f", bounds.size.width , bounds.size.height]); 

    CGFloat width_t = bounds.size.width; 

    bounds.size.width = [imagesArray count] * bounds.size.width; 

    self = [self initWithFrame:bounds]; 

    _width = width_t; 

    [self setBackgroundColor:[[UIColor alloc] initWithRed:0.2 green:0.1 blue:0.3 alpha:0.4]]; 

    if (self) { 
     [self setImages:imagesArray]; 
     [self setSlideTime:milliSeconds]; 
     [self setCaptions:captionArray]; 
     [self defaultLoad]; 
    } 

    self.scrollEnabled = YES; 

    return self; 
} 
+0

感謝您的更改,但仍然無法解決我的問題。圖像未顯示。實際上通過設置imageview的背景顏色,我開始知道不會添加hte UIImageView。這可以幫助什麼嗎? – 2013-03-18 14:13:41

+0

那麼我會檢查[HCIResultListViewController resizeImage:...]在defaultLoad循環中的輸出。它真的返回一個UIImage對象嗎?是否有適當的UIImage對象交給? – 2013-03-18 14:51:06

相關問題