Objective-C 将图像缩略图附加到视图的末尾
Objective-C append image thumbnails onto the end of a view
我正在尝试将图像附加到视图上并让它们在整齐的行中显示缩略图。我遇到的问题是,似乎所有图像都只是在角落里相互叠加,因为只有数组中的最后一张图像被显示。
- (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];
}
使用 UIStackView
的 addArrangedSubview
而不是 addSubview
。
并且您应该将所有按钮添加到同一个 UIStackView
。因此,将 UIStackView
的 属性 添加到您的 class,在 viewDidLoad
(或故事板)中对其进行初始化,然后将按钮添加到 [=16] 中的堆栈视图=].
我正在尝试将图像附加到视图上并让它们在整齐的行中显示缩略图。我遇到的问题是,似乎所有图像都只是在角落里相互叠加,因为只有数组中的最后一张图像被显示。
- (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];
}
使用 UIStackView
的 addArrangedSubview
而不是 addSubview
。
并且您应该将所有按钮添加到同一个 UIStackView
。因此,将 UIStackView
的 属性 添加到您的 class,在 viewDidLoad
(或故事板)中对其进行初始化,然后将按钮添加到 [=16] 中的堆栈视图=].