现在的位置: 首页 > 综合 > 正文

IOS突破限制–7

2017年10月20日 ⁄ 综合 ⁄ 共 1134字 ⁄ 字号 评论关闭

//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

抱歉!评论已关闭.