Created
February 26, 2014 06:14
-
-
Save keicoder/9224472 to your computer and use it in GitHub Desktop.
objective-c : using UIImagePickerController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using UIImagePickerController | |
//TWPhotosCollectionViewController.h | |
@interface TWPhotosCollectionViewController : UICollectionViewController | |
- (IBAction)cameraBarButtonItemPressed:(UIBarButtonItem *)sender; | |
@end | |
//TWPhotosCollectionViewController.m | |
#import "TWPhotosCollectionViewController.h" | |
#import "TWPhotoCollectionViewCell.h" //커스텀 컬렉션 뷰 셀 | |
//UIImagePickerController는 UIImagePickerControllerDelegate, UINavigationControllerDelegate 두 델리게이션 모두 필요함 | |
@interface TWPhotosCollectionViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> | |
@property (strong, nonatomic) NSMutableArray *photos; //Of UIImaqges | |
@end | |
@implementation TWPhotosCollectionViewController | |
- (NSMutableArray *)photos | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
if (!_photos) { | |
_photos = [[NSMutableArray alloc] init]; | |
} | |
return _photos; | |
} | |
#pragma mark - IBAction | |
- (IBAction)cameraBarButtonItemPressed:(UIBarButtonItem *)sender | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
//UIImagePickerController 오브젝트 | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker.delegate = self; //델리게이트 | |
//카메라 또는 포토앨범 선택 | |
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { | |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; | |
} else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { | |
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; | |
} | |
//present 뷰컨트롤러 | |
[self presentViewController:picker animated:YES completion:^{ | |
NSLog(@"Picker View done presenting"); | |
}]; | |
} | |
#pragma mark - UICollectionView 데이터 소스 | |
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
static NSString *CellIdentifier = @"Photo Cell"; | |
//커스텀 컬렉션 뷰 셀 사용 | |
TWPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier | |
forIndexPath:indexPath]; | |
cell.backgroundColor = [UIColor whiteColor]; | |
cell.imageView.image = self.photos[indexPath.row]; | |
return cell; | |
} | |
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
return [self.photos count]; | |
} | |
#pragma mark - UIImagePickerController 델리게이트 | |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
NSLog (@"Finish image picking"); | |
UIImage *image= info[UIImagePickerControllerEditedImage]; | |
if (!image) image = info[UIImagePickerControllerOriginalImage]; | |
[self.photos addObject:image]; | |
[self.collectionView reloadData]; | |
//이미지 피커 뷰 해제 | |
[self dismissViewControllerAnimated:YES completion:^{ | |
NSLog(@"Finished image picking"); | |
}]; | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
NSLog (@"Image picker cancelled"); | |
//이미지 피커 뷰 해제 | |
[self dismissViewControllerAnimated:YES completion:^{ | |
NSLog(@"Image picker view dismissed"); | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment