IOS突破限制--7

本文介绍了一个UICollectionView的数据源方法实现,展示了如何加载和显示照片文件,并提供了一些实用的代码片段,例如如何去除文件的路径扩展名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//UICollectionViewDataSource Methods (.m文件)
- (UICollectionViewCell *)collectionView:(UICollectionView *)
   collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  
  MKPhotoCell *cell = (MKPhotoCell*) [collectionView
  dequeueReusableCellWithReuseIdentifier:@"MKPhotoCell"
  forIndexPath:indexPath];
  
  NSString *photoName = [self.photosList objectAtIndex:indexPath.row];
  NSString *photoFilePath = [[self photosDirectory] 
   stringByAppendingPathComponent:photoName];
  cell.nameLabel.text =[photoName stringByDeletingPathExtension];
  UIImage *image = [UIImage imageWithContentsOfFile:photoFilePath];
  UIGraphicsBeginImageContext(CGSizeMake(128.0f, 128.0f));
  [image drawInRect:CGRectMake(0, 0, 128.0f, 128.0f)];
  cell.photoView.image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
 
  return cell;
}
之所以,贴出这段代码。因为这段代码有许多好的方法,可能会用到。

如:去掉后缀名

cell.nameLabel.text =[photoName stringByDeletingPathExtension];

其中的方法,photosDirectory

-(NSString*) photosDirectory {
  return [[[NSBundle mainBundle] resourcePath]
 stringByAppendingPathComponent:@"Photos"];
}

还有

self.photosList = [[NSFileManager defaultManager] 
   contentsOfDirectoryAtPath:[self photosDirectory] error:nil];

代码来自:ios7 pushing pushing the limits

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值