如何使用 plist 从服务器加载图像



我正在使用plist来存储应用程序中的数据,但我不希望数据存储在本地(很多图像),我希望图像上传到服务器上并能够从那里读取它们。可以使用 plist 吗?

下面是项目的 plist 结构:

<key></key>
        <string> Servings: 4
Preheat oven to 350˚F. Grease a 1 to 2-quart baking dish.
In a medium bowl, toss together the bread cubes and butter.
In a large bowl, toss together the apples, agave syrup, lemon juice, cinnamon, allspice, and salt.
Cover and bake 30 minutes. Uncover and bake 20 to 30 minutes longer or until bread cubes are browned.
</string>
        <key>smallImage</key>
        <string>s130.png</string>
        <key>ingredients</key>
        <string>2 cups cubed sourdough bread (½-inch cubes) 
        1 tablespoon melted butter 
        3 cups thinly sliced, peeled apples 
        ¼ cup agave syrup 
        2 teaspoons fresh lemon juice 
        ½ teaspoon ground cinnamon 
        ¼ teaspoon ground allspice 
        Salt to taste
</string>
        <key>title</key>
        <string>Apple Brown Betty
</string>
        <key>coverImage</key>
        <string>130.png</string>
    </dict>

以及我现在从 plist 加载数据的方式:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    recipes = [[BooksLibraryDao alloc] initWithLibraryName:@"TestData"];
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [recipes libraryCount];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Game Cell Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    cell.textLabel.text = [[recipes libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 14.0 ];
    cell.textLabel.font  = myFont;
    cell.imageView.image = [UIImage
                            imageNamed:[[recipes libraryItemAtIndex:indexPath.row] valueForKey:@"smallImage"]];
    cell.accessoryView = nil;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

要从服务器加载图像,您可以使用SDWEBIMAGE。下载此 github 链接并将其复制到您的项目中。现在你需要,

#import <SDWebImage/UIImageView+WebCache.h>

然后你可以这样做,

[YourImageView setImageWithURL:[NSURL URLWithString:@"Your URL link"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

其中占位符图像是默认图像,当图像在服务器上不可用时。希望这对您有所帮助!

最新更新