iPhone写真スクラップ

iPadのアプリ上にImagePickerから選んだ写真を表示していくiPadアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController () <UINavigationControllerDelegate, UIPopoverControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIPopoverController *pc;

@property (nonatomic, strong) NSMutableArray *photos;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor greenColor];

    self.view.layer.borderWidth = 20;

    self.view.layer.borderColor = [UIColor darkGrayColor].CGColor;

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.titleLabel.font = [UIFont boldSystemFontOfSize:40];

    [btn setTitle:@”Add” forState:UIControlStateNormal];

    [btn sizeToFit];

    btn.center = self.view.center;

    [self.view addSubview:btn];

    

    [btn addTarget:self action:@selector(showPopoverPicker) forControlEvents:UIControlEventTouchUpInside];

    

    self.photos = [NSMutableArray array];

}

– (void)showPopoverPicker

{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    picker.allowsEditing = NO;

    

    self.pc = [[UIPopoverController alloc] initWithContentViewController:picker];

    self.pc.delegate = self;

    [self.pc presentPopoverFromRect:CGRectMake(450, 500, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

– (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];

    if (img) {

        UIImageView *iv = [[UIImageView alloc] initWithImage:img];

        iv.frame = CGRectMake(0, 0, 100, 141.4);

        [self.view addSubview:iv];

        

        if (!self.photos) {

            self.photos = [NSMutableArray array];

        }

        iv.center = CGPointMake(self.photos.count * 110 + 80, 100);

        iv.layer.borderWidth = 5;

        iv.layer.borderColor = [UIColor whiteColor].CGColor;

        [self.photos addObject:iv];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end