最新消息:20210917 已从crifan.com换到crifan.org

给Your Second iOS App:BirdWatching添加支持用户自定义图片

iOS crifan 1607浏览 0评论

之前已经给Your Second iOS App的BirdWatching添加让列表可修改,可排序,现在继续去参考:Your Second iOS App -  Next Steps – Add More Functionality的建议:

Allow the user to add an image to a bird sighting. The app could allow the user to choose from a set of stock images or from the user’s own photos.

去添加支持用户自定义图片。


1. 从库中找到了Image View控件:

Image View

added  image view component

接着就是想办法编码控制添加图片了。

2.参考:

UIImageView Class Reference

Display An Image

去建立了对应的Outlet关联Image View和变量birdImageView,然后在ViewDidLoad中添加了一行代码:

    [self.birdImageView setImage:[UIImage imageNamed:@"default.jpg"]];

打算去显示图片,但是很明显,需要知道图片的位置才可以.

所以想要找到iPhone中默认的文件夹是哪里,以及有哪些函数和空间可以像Windows一样打开一个对话框供用户选择图片。

3.而关于如何支持,当用户点击图片时,弹出对话框,让用户选择自定义图片的折腾过程,参考:

【已解决】关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件

4.接下来,还需要让每个Bird的所对应的图片,显示到UITableView中。

关于如何给UITableView的UITableViewCell中添加图片的显示,还是要继续去参考官网的资料:

Table View Programming Guide for iOS

然后可以看出,其是符合:

Table View Styles and Accessory Views

中的“Figure 1-7  Table row style with subtitle under title”:

Figure 1-7  Table row style with subtitle under title

然后就去自己的程序中看看当前的cell是啥类型的,发现原来此处已经是上述的“UITableViewCellStyleSubtitle ”类型了,然后也可以设置对应的图片的:

def cell has support img

这样的话,后面的事情就方便了,就可以添加代码,把图片赋值过去即可。

5.经过一番写代码和调试,最后终于可以正常显示UITableViewCell中的图片了:

def show img in cell

然后新添加的cell中的图片,也可以显示出来了:

can set all values

最后添加出来的cell的效果是:

finnal cell show

这样,就可以实现了用户自定义选择图片,并且可以在列表中显示出来了。

如此,基本的功能,就实现了。

相关的核心部分的代码为:

返回时,把image参数加上:

- (IBAction)done:(id)sender {
    [[self delegate] addSightingViewControllerDidFinish:self name:self.birdNameInput.text location:self.locationInput.text date:self.datePicker.date image:self.birdImageView.image];
}

最后对于cell部分的显示,确保赋值了image的值:

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"BirdSightingCell";

   
    static NSDateFormatter *formatter = nil;

    if( formatter == nil)

    {

        formatter = [[NSDateFormatter alloc] init];

        [formatter setDateStyle:NSDateFormatterMediumStyle];

    }

   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   
    BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];

    [[cell textLabel] setText:sightingAtIndex.name];

    [[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]];

    cell.imageView.image = sightingAtIndex.image;

   
    return cell;

}

 

其他琐碎的,关于初始化,函数定义等部分的代码,就不列出来了。

 

【总结】

此处,从无到有的,在iOS的Simulator中实现用户可以自定义选择图片并在列表中显示的话,总的逻辑是:

1.关于如何可以弹出对话框并获得所选图片的逻辑,参考这里的总结:

【已解决】关于iOS/iPhone中的文件选择对话框,用于用户去选择图片等文件

2.获得所选图片后,想要在列表中显示的话,需要UITableViewCell是UITableViewCellStyleSubtitle的类型

3.然后返回添加的项时,把所选图片UIImage赋值给对应的UITableViewCell的imageView.image,就可以显示出对应的图片了。

转载请注明:在路上 » 给Your Second iOS App:BirdWatching添加支持用户自定义图片

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.184 seconds, using 23.40MB memory