获取已保存的图像并显示

时间:2015-06-15 06:23:08

标签: ios image path

我正在开发一个应用程序,其中我通过imagePicker存储了图像。现在我想在我的collectionView中显示相同的图像。我得到了回应作为路径

path=assets/image/155/155_557b1259b8a51.jpg

当我添加http://xyz.com.assets/image/155/155_557b1259b8a51.jpg时,它会显示错误。 如何通过这条路径获取图像。请帮助我。

1 个答案:

答案 0 :(得分:0)

如果你知道图像路径或名称,那么你可以实现类似的东西。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [[UICollectionViewCell alloc]initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50.0, 50.0)];

    [imageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"your photo name here" ofType:@"png"]]];//if Path is from main bundle
//else
// documentDirectory URL as
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:fileName];
[imageView setImage:[UIImage imageWithContentsOfFile:filePath];
}

[cell setBackgroundView:imageView];
NSLog(@"%@",cell);
NSLog(@"Index Path = %ld",(long)indexPath.row);
return cell;
}

您还需要将delegate and datasource UICollectionView设为

[yourCollectionViewObject setDelegate:self];// if you need to use didselect or like...
[yourCollectionViewObject setDataSource:self];

和另一个返回count ...的必需数据源方法。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return count;
}