gpt4 book ai didi

ios - UIImageView 未添加到 UIView

转载 作者:行者123 更新时间:2023-11-29 11:01:56 24 4
gpt4 key购买 nike

我正在尝试开发一个库,它获取一组图像并返回一个并排显示图像的 ScrollView 。

但是,当我添加这个 scrollView 时,我要返回到我的主视图,它不会添加图像。图片正确。

我的想法是使用一个 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

最佳答案

您的代码似乎有很多错误 - 特别是您的初始化程序。这是注释版本

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

否则,您的图片将全部放置在 ScrollView 高度下方的屏幕外。

(2)

你需要设置一个contentSize,这样scrollView才能滚动

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

另一个更笼统的评论是,这对于一些图像来说是可以的,但是你不希望制作一个带有大量屏幕外 ImageView 的 ScrollView ,因为你会耗尽内存。您实际上只需要三个,用于当前屏幕上的图像,以及左右两侧的上一张和下一张图像。这个想法是仅在您可能需要时加载您的图像,并像 tableView 的工作方式一样回收您的 imageView。

你应该看看Apple's Photoscroller sample app以及随附的 WWDC 2010/11 视频和 slides

不要担心有关平铺和缩放的所有细节,最好了解将对象创建保持在最低限度并在可行的情况下回收/重用对象的一般原则。

顺便说一句,您可能根本不需要自定义 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];
}

关于ios - UIImageView 未添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15478155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com