ELCImagePicker 学习总结(二)

来源:互联网 发布:蓝牙4.0软件 编辑:程序博客网 时间:2024/05/16 17:30
  • 头文件


#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface MyImagePickerViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UITableView *tableView;
    UIActivityIndicatorView *activity;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activity;

@end

  • 源文件

//
//  MyImagePickerViewController.m
//  MyImagePicker
//
//  Created by lisa liu on 4/14/15.
//  Copyright (c) 2015 lisa liu. All rights reserved.
//

#import "MyImagePickerViewController.h"

@interface MyImagePickerViewController ()

@property (nonatomic) NSMutableArray *assets;
@property (nonatomic, strong)ALAssetsLibrary* library;

@end

@implementation MyImagePickerViewController

@synthesize tableView;
@synthesize activity;
@synthesize assets;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.activity startAnimating];
    
    //Define Blocks Here
    
    void (^assetEnumerator) (ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
        
        if (result != nil) {
            
            NSLog(@"See Asset:%@", result);
            [self.assets addObject:result];
        }
    };

    
    void (^assetGroupEnumerator) ( ALAssetsGroup *, BOOL *) = ^( ALAssetsGroup *group, BOOL *stop){
    
        if (group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
        [self.tableView reloadData];
        [self.activity stopAnimating];
        [self.activity setHidden:YES];
    };
    self.assets = [[NSMutableArray alloc]init];
    ALAssetsLibrary *libraryL = [[ALAssetsLibrary alloc]init];
    self.library = libraryL;
    [self.library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                           failureBlock:^(NSError *error){
         NSLog(@"A problem occured");
     }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [assets count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    ALAsset *asset = [assets objectAtIndex:indexPath.row];
    [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
    [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row + 1]];
    return cell;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


0 0
原创粉丝点击