简体   繁体   English

没有将UIImageView添加到UIView

[英]UIImageView not added to UIView

I'm trying to develop a library which takes an array of images and returns a scrollView with the images each side by side. 我正在尝试开发一个库,该库采用图像数组并返回并排带有图像的scrollView。

However when I add this scrollView, which I'm returning to my main view, it doesn't add the images. 但是,当我添加此scrollView并返回主视图时,它不会添加图像。 Images are correct. 图像正确。

My idea is to use a scrollview with each image side by side and using the scroller to show like a slide show. 我的想法是使用每个图像并排的滚动视图,并使用滚动条像幻灯片一样显示。 Firstly, is this concept okay? 首先,这个概念可以吗?

Secondly, I do not understand why the images are not shown when I run the application in the simulator. 其次,我不明白为什么在模拟器中运行该应用程序时未显示图像。

Any more details required please ask. 需要更多详细信息请询问。

The code is as follows. 代码如下。 My header file : 我的头文件:

#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

My implementation file: 我的实施文件:

#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

There seems to be a lot wrong with your code - especially your initialiser. 您的代码似乎有很多错误-特别是您的初始化程序。 Here is a commented version 这是评论版

@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;
}

As your initialisation code needs some rearranging, I did not look too closely at defaultLoad - however I do have a couple of observations... 由于您的初始化代码需要重新安排,因此我对defaultLoad关注并不太多-但是,我确实有defaultLoad观察...

(1) (1)

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

Should be 应该

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

Otherwise your images are all placed offscreen beneath your scrollView's height. 否则,所有图像都将被放置在scrollView高度以下的屏幕外。

(2) (2)

You need to set a contentSize so that the scrollView can scroll 您需要设置一个contentSize,以便scrollView可以滚动

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

The other more general comment is that this is OK for a few images, but you wouldn't want to be making a scrollView with a large array of offscreen imageViews as you will eat up memory. 另一个比较笼统的评论是,对于一些图像来说这是可以的,但是您不希望使用带有大量屏幕外imageViews的scrollView来占用内存。 You actually only need three, for the image currently on screen, and the previous and next images to the left and right. 实际上,对于当前在屏幕上显示的图像,您只需要三个,而在左右图像上则需要上一个和下一个图像。 The idea is to load your images only when you may need them, and recycle your imageViews much the way a tableView works. 这个想法是仅在需要时才加载图像,并以tableView的工作方式回收imageViews。

You should take a look at Apple's Photoscroller sample app along with it's accompanying WWDC 2010/11 video(s) and slides 您应该看看Apple的Photoscroller示例应用程序以及随附的WWDC 2010/11视频和幻灯片

Don't worry about all of the details on tiling and zooming, it's good to get the general principle of keeping object creation to a minimum and recycling/reusing objects where feasible. 不必担心平铺和缩放的所有细节,最好将通用的原则保持在最低限度,并在可行的情况下回收/重用对象。

By the way, you may not need a custom scrollView object at all: you can achieve most of what you are after in a few lines of code from your calling viewController object. 顺便说一句,您可能根本不需要自定义的scrollView对象:您可以从调用viewController对象的几行代码中完成大部分工作。 eg... 例如...

- (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];
}
- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM