我在故事板UITableView
中创建了原型单元格,TableViewController和UITableViewCell
。现在我想选择手机图库中的所有图片并将一个或多个图片保存到数据库中。但在这一刻,我需要实现添加图片到tableview。我是iOS的初学者。谁能告诉我怎么能做到这一点?给我一个链接或代码?感谢您的提前如何将图像添加到手机图库中的UITableView
0
A
回答
2
您可以使用ALAssetsLibrary
在AssetsLibrary framework
从手机库中获取所有图像。将每个图像保存到一个数组中。
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil) {
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
NSURL *url= (NSURL*) [[result defaultRepresentation] url];
[library assetForURL:url
resultBlock:^(ALAsset *asset) {
UIImage *img = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
[imgArray addbject:img];
}
failureBlock:^(NSError *error){ NSLog(@"operation failed"); } ];
}
}
};
然后,您可以使用此数组保存到数据库或在tableview中显示。 对于在TableView中使用,您需要在原型单元格中添加UIImageView
,然后相应地将阵列中的图像设置为单元格。您的表格视图委托方法将如下所示。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [imgArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellId = @"ImageCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
}
UIImage *img = [imgArray objectAtIndex:indexPath.row];
cell.imgView.image = img;
return cell;
}
1
如果您想选择从图库中的图像,并将其展示给tableview中,你可以使用自定义的ImageView中的tableView或CustomCell ImageView的
首先,你需要从图库中选择图像并将其保存到数组
该分配和初始化在viewDidLoad方法阵列
ViewController.m
#import "ViewController.h"
@interface ViewController()
{
NSMutableArray *arrayImage;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
arrayImage = [[NSMutableArray alloc]init];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
imageView.image=image;
[arrayImage addObject:image];
picker.delegate =self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
之前
然后在的tableView
如果CustomCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell==nil)
{
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
cell = nib[0];
}
cell.galleryImageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}
如果使用默认表视图细胞
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.imageView.image = [arrayImage objectAtIndex:indexPath.row];
return cell;
}
相关问题
- 1. 将图像保存到从图库加载的手机
- 2. 添加图像到uitableview cell
- 3. 如何将iPhone图库中的图像添加到数组IOS
- 4. 无法将图像添加到图库
- 5. Fancybox将图像添加到图库
- 6. 将图像动态添加到图库
- 7. 如何将3个图像添加到UITableView的每一行?
- 8. 如何将位图保存到手机的图库中?
- 9. 如何将图像添加到位图
- 10. 如何将图像添加到视图
- 11. 如何将Android手机上的图像链接到数据库
- 12. 用手机将图像保存到图库中
- 13. 如何将捕获的图像保存到Android手机图库中?
- 14. 如何将图像添加到TextView中
- 15. 如何使用PHPhotoLibrary将图像导出到手机库
- 16. 将图像添加到Android手机顶部条上
- 17. 将随机图像添加到标签
- 18. 如何在OpenCv C++中将图像添加到图像中
- 19. jQuery手机:动态添加图像
- 20. Windows手机为图像添加边框
- 21. 将图标图像添加到UITableview阵列
- 22. 如何将按钮添加到通用Android图像加载器中的图库?
- 23. 将库存图像添加到Android Studio
- 24. Android将图像添加到数据库
- 25. 如何将内存中的png图像添加到图中?
- 26. 如何将其他手机外观添加到Android Studio图像捕获工具?
- 27. 如何将图像添加到图像视图
- 28. 如何将多个图像添加到图像视图
- 29. 添加图像到jar库
- 30. 如何将UITableView作为子视图添加到xib中的UIView?
试试这个http://stackoverflow.com/questions/12633843/get-all -of-the-pictures-from-an-iphone-photolibrary-in-an-an-an-array-using-assetslibr – sanjeet
kenzolek检查我的回答 – user3182143
我的回答对你有帮助吗? – user3182143