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