我在故事板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