【发布时间】:2016-05-05 14:58:56
【问题描述】:
我正在尝试将图像附加到视图上,并让它们在整齐的行中显示缩略图。我遇到的问题是,似乎所有图像都只是在角落中相互叠加,因为仅显示数组中的最后一张图像。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.notesField.delegate = self;
NSString *userName = ((ontracAppDelegate*)[UIApplication sharedApplication].delegate).userName;
self.textField.text = [NSString stringWithFormat:@"Date Created: %@ \nCreated By: %@",[NSDate date], userName];
if (self.noteText != nil) {
self.notesField.text = self.noteText;
self.notesField.editable = NO;
self.textLabel.hidden = TRUE;
}
[self.view resignFirstResponder];
if ([self.imageArray count] != 0) {
for (UIImage *image in imageArray) {
NSLog(@"Image %@", image);
[self addImageToScreen:image];
}
}
NSLog(@"imageArray %@", imageArray);
NSLog(@"self.textField.text %@", self.textField.text );
NSLog(@"self.notesField.text %@", self.notesField.text);
}
-(void) addImageToScreen:(UIImage*) image;
{
int adjustHeight = 0;
int adjustWidth = 10;
int imagesInARow = 7;
int imageHeight = 75;
int imageWidth = 75;
int count = [self.imageArray count];
self.numberOfPhotosLabel.text = [NSString stringWithFormat:@"%i",count];
if (count > 1 && count < imagesInARow) {
adjustHeight = 0;
adjustWidth = (20 * [self.imageArray indexOfObject:image]) + ([self.imageArray indexOfObject:image] * imageWidth);
}
UIButton* container = [[UIButton alloc] init ];
CGRect frame = CGRectMake(adjustWidth, 5, imageWidth, imageHeight);
[container setTag:[self.imageArray indexOfObject:image]];
[container setImage:image forState:UIControlStateNormal];
[container setFrame:frame];
[container addTarget:self action:@selector(displayFullScreenImage:) forControlEvents:UIControlEventAllTouchEvents];
UIStackView *photosView = [[UIStackView alloc] initWithFrame:frame];
container.frame = photosView.bounds;
[photosView addSubview:container];
[self.view addSubview:photosView];
}
【问题讨论】:
-
您在哪里将图像添加到
imageArray?从代码中我会假设这个数组总是空的。 -
这是在别处添加的,但我检查了数组,它不是空的。
-
好的,我只是想确认一下。然后,检查我的答案。
标签: objective-c addsubview uistackview