简体   繁体   English

Objective-C-如何将多个图像添加到标签上显示的数组?

[英]objective-c - How to add multiple images to an array shown on a label?

I am trying to show sometimes photos and sometimes facts in a label. 我试图在标签上显示有时的照片和有时的事实。 For that I've an array with facts in my .m file including an arcm4 for randomly generated content. 为此,我在.m文件中有一个包含事实的数组,其中包括用于随机生成的内容的arcm4。

- (instancetype) init {
    self = [super init];
    if (self) {
        _Facts = @[
                   @"The length of an elephant is the same as the tongue of a blue whale.",
                   @"The crocodile’s tongue is unmovable, as it is attached to the roof of its mouth."
                   ];
    }
    return self;
}

- (void)populateLabel:(UILabel *)Label {
    int random = arc4random_uniform(@(_aFacts.count).intValue);
    NSObject * element = _aFacts[random];
    if ([element isKindOfClass:NSString.class]) {
        //        NSLog(@"Will add string");
        Label.attributedText = nil;
        Label.text = (NSString*) element;
    } else if ([element isKindOfClass:NSAttributedString.class]) {
        //        NSLog(@"Will add image");
        Label.text = nil;
        Label.attributedText = (NSAttributedString*)element;
    } else {
        NSLog(@"_aFacts array is supposed to contain only NSString(s) and NSAttributedString(s). Instead a %@ has been found at index %d", NSStringFromClass(element.class), random);
    }
}

Then I wanted to also include images by adding this code: 然后,我想通过添加以下代码来包含图像:

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"196H"];
NSAttributedString *attributedString = [NSAttributedString attributedStringWithAttachment:attachment];

And at the and of the array after the last item this: 在最后一项之后的和数组中:

, attributedString

I'm calling this code in my viewController file: 我在我的viewController文件中调用此代码:

[self.aFactBook populateLabel:self.Label];

So this code works well but 所以这段代码很好用,但是

  1. I need to include over 200+ images 我需要包含200多张图片
  2. Is there any better solution than working like this? 有没有比这样更好的解决方案了?

Thanks for your help! 谢谢你的帮助!

It depends on how much performance means to you. 这取决于性能对您意味着什么。 Sometimes it is really worthy to cache 200 images so you can operate them well, sometimes 200 images could be extended to 10 000 images and then you'd want to store their names and load them only when needed instead of keeping all of them in memory. 有时确实值得缓存200张图像,因此您可以很好地操作它们,有时200张图像可以扩展到10,000张图像,然后您想要存储它们的名称并仅在需要时加载它们,而不是将所有图像都保留在内存中。

Maybe you should consider using something like database whether it is SQLite or CoreData. 也许您应该考虑使用类似SQLite或CoreData之类的数据库。

The best way to keep your data is to use databases. 保留数据的最佳方法是使用数据库。 I personally recommend SQLite, it is very easy and powerful DMS. 我个人推荐SQLite,它是非常简单且功能强大的DMS。 Also, if you need further expand in the future with the database you will not need to change the code ,just the database's records. 另外,如果将来需要进一步扩展数据库,则无需更改代码,只需更改数据库的记录即可。

You can use BLOB( data type ) for images and VARCHAR for your facts( those data types are for the building of the database's table. 您可以对图像使用BLOB(数据类型),对事实使用VARCHAR(这些数据类型用于数据库表的构建)。

If you are satisfy with my answer, don't forget to thumb up! 如果您对我的回答感到满意,请不要忘记竖起大拇指!

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

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