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

时间:2014-04-07 08:36:35

标签: ios uiimageview 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;

}

1 个答案:

答案 0 :(得分:0)

对于来自服务器的加载图像,您可以使用SDWEBIMAGE。下载this github link并将其复制到您的项目中。 现在你需要,

#import <SDWebImage/UIImageView+WebCache.h>

然后你可以这样做,

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

当服务器上没有图像时,Placeholderimage是默认图像。 希望这能帮到你!