用UIImagePickerViewController自定義相機界面

 
 
1.設定p_w_picpathPacker參數

 
// Transform values for full screen support:
#define CAMERA_TRANSFORM_X 1
// this works for iOS 4.x
#define CAMERA_TRANSFORM_Y 1.24299 

-(void)viewWillAppear:(BOOL)animated{
    
    UIImagePickerController *p_w_picpathPickerController = [[UIImagePickerController alloc] init];
p_w_picpathPickerController.delegate = self;

p_w_picpathPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
p_w_picpathPickerController.showsCameraControls = NO;
p_w_picpathPickerController.navigationBarHidden = YES;
p_w_picpathPickerController.wantsFullScreenLayout = YES;
p_w_picpathPickerController.cameraViewTransform = CGAffineTransformScale(p_w_picpathPickerController.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
    cameraViewController = [[CameraOverlayViewController alloc]initWithNibName:@"CameraOverlayViewController" bundle:nil];
    p_w_picpathPickerController.cameraOverlayView = cameraViewController.view;
    cameraViewController.pickerController = p_w_picpathPickerController;
    [self presentModalViewController:p_w_picpathPickerController animated:NO];
    [p_w_picpathPickerController release];
}

2.代理方法

-(void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissModalViewControllerAnimated:NO];
    UIImage* p_w_picpath = [info valueForKey:UIImagePickerControllerOriginalImage];
    BeautifyPhotoViewController* beautifyPhotoViewController = [[BeautifyPhotoViewController alloc]initWithNibName:@"BeautifyPhotoViewController" bundle:nil];
    beautifyPhotoViewController.photoImage = p_w_picpath;
    [self.navigationController pushViewController:beautifyPhotoViewController animated:YES];
    [beautifyPhotoViewController release];
}

3.overlayView對應的controller

#import "CameraOverlayViewController.h"

@interface CameraOverlayViewController ()

@end

@implementation CameraOverlayViewController

@synthesize pickerController;
@synthesize cameraScaleBtn;
@synthesize titleImageView;
@synthesize flashModeBtn;
@synthesize deviceModeBtn;
@synthesize photoBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self getAllPhotoImages];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isOneToOne = YES;
    p_w_picpathArray = [[NSMutableArray alloc]init];
    shadowView = [[ShadowView alloc]initWithRect:CGRectMake(0, 80, 320, 320)];
    [self.view addSubview:shadowView];
    [self.view sendSubviewToBack:shadowView];
    [shadowView release];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
//閃光燈
-(IBAction)cameraTorchOn:(id)sender{
    if (pickerController.cameraFlashMode ==UIImagePickerControllerCameraFlashModeAuto) {
        pickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
    }else {
        pickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    }
}

//前後攝像頭
- (IBAction)swapFrontAndBackCameras:(id)sender {
    if (pickerController.cameraDevice ==UIImagePickerControllerCameraDeviceRear ) {
        pickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    }else {
        pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    }
}
//改變拍攝比例
-(IBAction)changeCameraScale:(id)sender{
    if (isOneToOne) {
        [shadowView changeRect:CGRectMake(0, 0, 320, 428)];
        [cameraScaleBtn setImage:[UIImage p_w_picpathNamed:@"font_-scale43.png"] forState:UIControlStateNormal];
        titleImageView.alpha = 0.2;
        flashModeBtn.alpha = 0.5;
        deviceModeBtn.alpha = 0.5;
        isOneToOne = NO;
    }else {
        [shadowView changeRect:CGRectMake(0, 80, 320, 320)];
        [cameraScaleBtn setImage:[UIImage p_w_picpathNamed:@"font_-scale11.png"] forState:UIControlStateNormal];
        titleImageView.alpha = 1;
        flashModeBtn.alpha = 1;
        deviceModeBtn.alpha = 1;
        isOneToOne = YES;  
    }
}

- (IBAction)enterPhotoAlbum:(id)sender {
    PhotoAlbumViewController* photoAlbumViewController = [[PhotoAlbumViewController alloc]initWithNibName:@"PhotoAlbumViewController" bundle:nil];
    [self presentModalViewController:photoAlbumViewController animated:YES];
    
}

//拍攝照片
-(IBAction)takeThePic:(id)sender{
    [pickerController takePicture];
}

-(IBAction)backToHome:(id)sender{
    [pickerController dismissModalViewControllerAnimated:NO];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"backToHome" object:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [cameraScaleBtn release];
    [flashModeBtn release];
    [deviceModeBtn release];
    [titleImageView release];
    [super dealloc];
}

-(void)getAllPhotoImages{
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
        
        NSLog(@"error occour =%@", [myerror localizedDescription]);
    };
    ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){
        if (result!=NULL) {
            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [p_w_picpathArray addObject:result];
                UIImage *img=[UIImage p_w_picpathWithCGImage:result.thumbnail];
                [photoBtn setImage:img forState:UIControlStateNormal];
            }
        }
    };
    ALAssetsLibraryGroupsEnumerationResultsBlock
    
    libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
        
        if (group == nil) {
            return;
        }
        if (group!=nil) {
            [group enumerateAssetsUsingBlock:groupEnumerAtion];
        }
    };
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
    
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
     
                           usingBlock:libraryGroupsEnumeration 
     
                         failureBlock:failureblock];
    [library release];
}
@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章