http://blog.sina.com.cn/s/blog_aedec703010181sh.html
1.设定imagePacker参数
// 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
*imagePickerController = [[UIImagePickerController
alloc
]
init
];
imagePickerController.
delegate =
self;
imagePickerController.sourceType
= UIImagePickerControllerS ourceTypeCamera
;
imagePickerController.
showsCameraControls =
NO;
imagePickerController.
navigationBarHidden =
YES;
imagePickerController.
wantsFullScreenLayout =
YES;
imagePickerController.cameraViewTransform
= CGAffineTransformScale
(imagePickerController.cameraViewTransform
,
CAMERA_TRANSFORM_X
,
CAMERA_TRANSFORM_Y
);
cameraViewController
= [[
CameraOverlayViewController alloc
]initWithNibName
:
@"CameraOverlayViewController" bundle
:
nil
];
imagePickerController.cameraOverlayView
= cameraViewController
.view
;
cameraViewController
.pickerController
= imagePickerController;
[
self presentModalViewControll er
:imagePickerController animated
:
NO
];
[imagePickerController
release];
}
2.代理方法
-(
void)imagePickerController:(
UIImagePickerController *)picker didFinishPickingMediaWit hInfo:(
NSDictionary *)info{
[picker dismissModalViewControll erAnimated
:
NO
];
UIImage
* image = [info valueForKey
:UIImagePickerControllerO riginalImage
];
BeautifyPhotoViewControl ler
* beautifyPhotoViewController = [[BeautifyPhotoViewControl ler
alloc
]
initWithNibName
:
@"BeautifyPhotoViewController"
bundle
:
nil
];
beautifyPhotoViewControl ler.
photoImage = image;
[
self
.navigationController pushViewController
:beautifyPhotoViewController animated
:
YES
];
[beautifyPhotoViewControl ler
release];
}
3.overlayView对应的controller
#import "CameraOverlayViewControl ler.h"
@interface CameraOverlayViewControl ler
()
@end
@implementation CameraOverlayViewControl ler
@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
;
imageArray
= [[
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
==UIImagePickerControllerC ameraFlashModeAuto
) {
pickerController
.cameraFlashMode
= UIImagePickerControllerC ameraFlashModeOn
;
}
else {
pickerController
.cameraFlashMode
= UIImagePickerControllerC ameraFlashModeOff
;
}
}
//前后摄像头
- (
IBAction)swapFrontAndBackCameras:(
id)sender {
if
(pickerController
.cameraDevice
==UIImagePickerControllerC ameraDeviceRear
) {
pickerController
.cameraDevice
= UIImagePickerControllerC ameraDeviceFront
;
}
else {
pickerController
.cameraDevice
= UIImagePickerControllerC ameraDeviceRear
;
}
}
//改变拍摄比例
-(
IBAction)changeCameraScale:(
id)sender{
if
(isOneToOne
) {
[shadowView changeRect
:CGRectMake
(
0
,
0
,
320
,
428
)];
[cameraScaleBtn setImage
:[
UIImage imageNamed
:
@"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 imageNamed
:
@"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 dismissModalViewControll erAnimated
:
NO
];
[[
NSNotificationCenter defaultCenter
] postNotificationName
:
@"backToHome" object
:
nil
];
}
- (
BOOL)shouldAutorotateToInterf aceOrientation:(
UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation ==
UIInterfaceOrientationPortrait);
}
- (
void)dealloc
{
[cameraScaleBtn release
];
[flashModeBtn release
];
[deviceModeBtn release
];
[titleImageView release
];
[
super
dealloc];
}
-(
void)getAllPhotoImages{
ALAssetsLibraryAccessFai lureBlock
failureblock = ^(NSError
*myerror){
NSLog
(
@"error occour =%@"
, [myerror localizedDescription
]);
};
ALAssetsGroupEnumeration ResultsBlock
groupEnumerAtion = ^(ALAsset
*result, NSUInteger
index,
BOOL
*stop){
if (result!=
NULL) {
if
([[result valueForProperty
:ALAssetPropertyType
] isEqualToString
:ALAssetTypePhoto
]) {
[
imageArray
addObject:result];
UIImage *img=[
UIImage
imageWithCGImage:result.
thumbnail];
[photoBtn setImage
:img forState
:UIControlStateNormal
];
}
}
};
ALAssetsLibraryGroupsEnu merationResultsBlock
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