简体   繁体   English

如何根据随机NSString将图像加载到UIImageView?

[英]How can I load a image to UIImageView depending on random NSString?

I read a book on Objective-C and made ​​a couple of simple applications, but still can't figure out how to do some easy things. 我读了一本关于Objective-C的书,做了两个简单的应用程序,但仍然不知道如何做一些简单的事情。 For example, there are ten names of books and ten covers to them.I want make appear one random book name on the screen with it's cover. 例如,有十本书的名字和十本书的封面。我想让它的封面在屏幕上出现一个随机的书名。 I can write something like this: 我可以这样写:

NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSString* book= bookNames[arc4random()%bookNames.count];

Is it right? 这样对吗? How can I load an image of cover to UIImageView depending on book name? 如何根据书名将封面图像加载到UIImageView?

You need another NSArray where you have to store names of your images, on the same index as your bookNames. 您需要另一个NSArray ,您必须在其中存储图像名称,该图像名称与bookNames相同。 For example: 例如:

//Your Arrays bookNames for names and bookImages for name of your images
NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSArray* bookImages = @[@"Harry-Potter.jpg", @"Atlas-Shrugged.jpg", @"The-Financier.jpg"];

//Setting a randomindex for your book
NSInteger yourRandomIndex = arc4random_uniform(bookNames.count);

//Getting your imageName and your bookName for your book at randomIndex
NSString* bookName= bookNames[yourRandomIndex];
NSString* bookImage= bookImages[yourRandomIndex];

//Setting your ImageView image and your bookName in a label from your strings
[imageView setImage:[UIImage imageNamed:bookImage]];
bookName.text = bookName;

In cases like these, I usually create a (small) class, let's call it Books, with two properties: 在这种情况下,我通常创建一个(小)类,我们将其称为Books,它具有两个属性:

@property (nonatomic, strong) NSString *bookTitle;
@property (nonatomic, strong) NSString *bookImage;

Create a static method that returns a new book object: 创建一个返回新书对象的静态方法:

+ (Books *)createBookWithTitle:(NSString *)title andImage:(NSString *)image
{
    Books *book = [[Books alloc]init];

    book.bookTitle = title;
    book.bookImage = image;

    return book;
}

Then, you create an array, and add Books objects to it: 然后,创建一个数组,并向其添加Books对象:

NSArray *books = @[[Books createBookWithTitle:@"Harry Potter" andImage:@"HarryPotter.png"],
                   [Books createBookWithTitle:@"Atlas Shrugged" andImage:@"Atlas Shrugged.png"],
                       ];

Then, you can just use: 然后,您可以使用:

Books *selectedBook = books[randomNumber];

NSString *title = selectedBook.bookTitle;
UIImage *image = selectedBook.bookImage;

It looks like overkill, but it clean and easy to work with. 看起来有些过分,但干净且易于使用。 They pair very nicely that way. 他们以这种方式配对非常好。

UIImageView *yourImageView = ...
    NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSString* book= bookNames[arc4random()%bookNames.count];
[yourImageView setImage:[UIImage imageNamed:book]];

Hope this helps 希望这可以帮助

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

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