1.NSData与UIImage ,NSMutableArray 的转换.
2.使用NSUserDefaults 存储图片.
3.动态加载ScrollView中的图片.
4.对图片单击与双击以及多击事件的响应.
5调用系统相机和照片库的功能.
一
首先导入MWPhotoBrowser 开源库.
二
导入UIImage类别
//
// UIImage+Utilities.h
// FoodFlow
//
// Created by Kishikawa Katsumi on 11/09/05.
// Copyright (c) 2011 Kishikawa Katsumi. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UIImage(Utilities)
+ (UIImage *)fixOrientation:(UIImage *)aImage;
- (CGRect)convertCropRect:(CGRect)cropRect;
- (UIImage *)croppedImage:(CGRect)cropRect;
- (UIImage *)resizedImage:(CGSize)size imageOrientation:(UIImageOrientation)imageOrientation;
- (UIImage *)resizedImageWithMaxSize:(CGSize)maxSize imageOrientation:(UIImageOrientation)imageOrientation;
@end
//
// UIImage+Utilities.m
// FoodFlow
//
// Created by Kishikawa Katsumi on 11/09/05.
// Copyright (c) 2011 Kishikawa Katsumi. All rights reserved.
//
#import "UIImage+Utilities.h"
@implementation UIImage(Utilities)
+(UIImage *)fixOrientation:(UIImage *)aImage {
// No-op if the orientation is already correct
if (aImage.imageOrientation == UIImageOrientationUp)
return aImage;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), 0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
- (CGRect)convertCropRect:(CGRect)cropRect {
UIImage *originalImage = self;
CGSize size = originalImage.size;
CGFloat x = cropRect.origin.x;
CGFloat y = cropRect.origin.y;
CGFloat width = cropRect.size.width;
CGFloat height = cropRect.size.height;
UIImageOrientation imageOrientation = originalImage.imageOrientation;
if (imageOrientation == UIImageOrientationRight || imageOrientation == UIImageOrientationRightMirrored) {
cropRect.origin.x = y;
cropRect.origin.y = size.width - cropRect.size.width - x;
cropRect.size.width = height;
cropRect.size.height = width;
} else if (imageOrientation == UIImageOrientationLeft || imageOrientation == UIImageOrientationLeftMirrored) {
cropRect.origin.x = size.height - cropRect.size.height - y;
cropRect.origin.y = x;
cropRect.size.width = height;
cropRect.size.height = width;
} else if (imageOrientation == UIImageOrientationDown || imageOrientation == UIImageOrientationDownMirrored) {
cropRect.origin.x = size.width - cropRect.size.width - x;;
cropRect.origin.y = size.height - cropRect.size.height - y;
}
return cropRect;
}
- (UIImage *)croppedImage:(CGRect)cropRect {
CGImageRef croppedCGImage = CGImageCreateWithImageInRect(self.CGImage ,cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:croppedCGImage scale:1.0f orientation:self.imageOrientation];
CGImageRelease(croppedCGImage);
return croppedImage;
}
- (UIImage *)resizedImage:(CGSize)size imageOrientation:(UIImageOrientation)imageOrientation {
CGSize imageSize = self.size;
CGFloat horizontalRatio = size.width / imageSize.width;
CGFloat verticalRatio = size.height / imageSize.height;
CGFloat ratio = MIN(horizontalRatio, verticalRatio);
CGSize targetSize = CGSizeMake(imageSize.width * ratio, imageSize.height * ratio);
UIGraphicsBeginImageContextWithOptions(size, YES, 1.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
if (imageOrientation == UIImageOrientationRight || imageOrientation == UIImageOrientationRightMirrored) {
transform = CGAffineTransformTranslate(transform, 0.0f, size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
} else if (imageOrientation == UIImageOrientationLeft || imageOrientation == UIImageOrientationLeftMirrored) {
transform = CGAffineTransformTranslate(transform, size.width, 0.0f);
transform = CGAffineTransformRotate(transform, M_PI_2);
} else if (imageOrientation == UIImageOrientationDown || imageOrientation == UIImageOrientationDownMirrored) {
transform = CGAffineTransformTranslate(transform, size.width, size.height);
transform = CGAffineTransformRotate(transform, M_PI);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake((size.width - targetSize.width) / 2, (size.height - targetSize.height) / 2, targetSize.width, targetSize.height), self.CGImage);
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
//将图片限定在一个最大的size内
- (UIImage *)resizedImageWithMaxSize:(CGSize)maxSize imageOrientation:(UIImageOrientation)imageOrientation {
CGSize imageSize = self.size;
if(imageSize.width > maxSize.width || imageSize.height>maxSize.height){
//CGSize imageSize = self.size;
NSLog(@"resized with max size");
CGFloat horizontalRatio = maxSize.width / imageSize.width;
CGFloat verticalRatio = maxSize.height / imageSize.height;
CGFloat ratio = MIN(horizontalRatio, verticalRatio);
CGSize size = CGSizeMake(imageSize.width*ratio, imageSize.height*ratio);
CGSize targetSize = CGSizeMake(imageSize.width * ratio, imageSize.height * ratio);
UIGraphicsBeginImageContextWithOptions(size, YES, 1.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
if (imageOrientation == UIImageOrientationRight || imageOrientation == UIImageOrientationRightMirrored) {
transform = CGAffineTransformTranslate(transform, 0.0f, size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
} else if (imageOrientation == UIImageOrientationLeft || imageOrientation == UIImageOrientationLeftMirrored) {
transform = CGAffineTransformTranslate(transform, size.width, 0.0f);
transform = CGAffineTransformRotate(transform, M_PI_2);
} else if (imageOrientation == UIImageOrientationDown || imageOrientation == UIImageOrientationDownMirrored) {
transform = CGAffineTransformTranslate(transform, size.width, size.height);
transform = CGAffineTransformRotate(transform, M_PI);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake((size.width - targetSize.width) / 2, (size.height - targetSize.height) / 2, targetSize.width, targetSize.height), self.CGImage);
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}else {
return self;
}
}
@end
ViewController.h
//
// ViewController.h
// SimpleDemo
//
// Created by skychi on 13-5-15.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "imagePickerUtil.h"
#import "MWPhotoBrowser.h"
@interface ViewController : UIViewController<ImagePickerUitlDelegate,UIGestureRecognizerDelegate ,UIScrollViewDelegate,MWPhotoBrowserDelegate,UIActionSheetDelegate>{
NSMutableArray *imageArrays;//在ScrollView中的图片数组.
IBOutlet UIScrollView *myScrollView;
NSUInteger imageIndex ;//图片索引
NSMutableArray * _photos;//在图片浏览器的图片数组.
IBOutlet UIImageView *userBackground;
IBOutlet UIImageView *userIcon;
BOOL isUserIcon;
BOOL isUserBg;
BOOL isBarBtn;
}
@property(assign , nonatomic) int tapCount;
@end
ViewController.m
//
// ViewController.m
// SimpleDemo
//
// Created by skychi on 13-5-15.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "ViewController.h"
#define USER_ICON @"USERICON"
#define USER_BACKGROUND @"USERBACKGROUND"
#define IMAGE_ARRAYS @"IMAGEARRAYS"
@interface ViewController ()
-(void)setImageView:(UIImageView *) imageView ;
-(void)doubleTapImageView:(UIImageView *)imageView;
@end
@implementation ViewController
@synthesize tapCount;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// imageArrays = [[NSMutableArray alloc]init];
_photos = [[NSMutableArray alloc]init];
isUserBg = NO;
isUserIcon = NO;
isBarBtn = NO;
self.title = @"Setting";
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"AddPics" style:UIBarButtonItemStyleDone target:self action:@selector(selectPics)];
self.navigationItem.rightBarButtonItem = rightBtn;
[self.view addSubview:myScrollView];
[self setImage];
[self showPic];
[self setImageView:userIcon];
[self setImageView:userBackground];
// [self doubleTapImageView:userIcon];
// Do any additional setup after loading the view from its nib.
}
-(void) selectPics {
isBarBtn = YES;
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择方式" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"图片库", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
actionSheet.tag = 1 ;
[actionSheet showInView:self.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
ImagePickerUitl *picker = [[ImagePickerUitl alloc] init];
picker.delegate = self;
switch (buttonIndex) {
case 0:
[picker pickeImageWithDefaultEditWithUIViewController:self inEditMode:YES inCaptureMode:YES];
break;
case 1:
[picker pickeImageWithDefaultEditWithUIViewController:self inEditMode:YES inCaptureMode:NO];
break;
default:
break;
}
}
-(void)selectImage:(UITapGestureRecognizer *)recognizer{
UIImageView *imageView = (UIImageView *)recognizer.view;
if (self.tapCount == 1 ) {
if (imageView.tag == 1000) {
isUserIcon = YES;
}else if(imageView.tag == 1001 ){
isUserBg = YES;
}
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择方式" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"图片库", nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
actionSheet.tag = 2 ;
[actionSheet showInView:self.view];
}
}
-(void)imagePickerUtil:(ImagePickerUitl *)util didFinishPickingImage:(UIImage *)image{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
if(isUserBg){
[userBackground setImage:image];
// NSData *userIconData = UIImagePNGRepresentation(image);
[userDefault setObject:UIImagePNGRepresentation(image) forKey:USER_BACKGROUND];
isUserBg = NO;
}
else if(isUserIcon){
[userIcon setImage:image];
//将UIimage 转化为NSData.
//NSData *userBgData = UIImagePNGRepresentation(image);
[userDefault setObject:UIImagePNGRepresentation(image) forKey:USER_ICON];
isUserIcon = NO;
}
else if (isBarBtn) {
[imageArrays addObject:image];
NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:imageArrays];
[userDefault setObject:userData forKey:IMAGE_ARRAYS];
[self showPic];
isBarBtn = NO;
}
[userDefault synchronize];
}
-(void)setImage{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userIcon setImage:[UIImage imageWithData:[userDefault objectForKey:USER_ICON]]];
[userBackground setImage:[UIImage imageWithData:[userDefault objectForKey:USER_BACKGROUND]]];
}
-(void)showPic{
//将NSData转化为NSMutableArray.
NSUserDefaults * defaults=[NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray=[defaults objectForKey:IMAGE_ARRAYS];
//这里需要对NSData对象进行判断,否则,可能出现未知bug.
if (dataRepresentingSavedArray!=nil) {
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
imageArrays = [[NSMutableArray alloc] initWithArray:oldSavedArray];
}
}else{
imageArrays = [[NSMutableArray alloc] init];
}
int count = [imageArrays count];
if (count<= 0 ) {
return;
}
//将之前的子视图移除.
for (UIImageView *imageview in [myScrollView subviews]) {
[imageview removeFromSuperview];
}
for (int i = 0 ; i < [imageArrays count]; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithImage:[imageArrays objectAtIndex:i]];
imageView.frame = CGRectMake(150 *i + 20*(i+1), 20, 150, 150);
// imageView.frame = CGRectMake(85, 150 *i + 20*(i+1), 150, 150);
imageView.userInteractionEnabled = YES;
imageView.tag = i ;
//添加子视图.
[self imageAction:imageView];
[myScrollView addSubview:imageView];
[myScrollView setContentSize:CGSizeMake(150 *(i+1) + 20*(i+2), myScrollView.frame.size.height)];
// [myScrollView setContentSize:CGSizeMake(320,150 *(i+1) + 20*(i+2) )];
[imageView release];
}
NSLog(@"After: myScrollView%i个子视图" , [[myScrollView subviews] count]);
}
-(void) imageAction:(UIImageView *)imageView{
UITapGestureRecognizer *recongizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showImage:)];
[recongizer setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:recongizer];
[recongizer release];
}
-(void) showImage:(UITapGestureRecognizer *) recognizer {
UIImageView *v = (UIImageView*)recognizer.view;
[_photos removeAllObjects];
for (int i = 0; i<[imageArrays count]; i++) {
[_photos addObject:[MWPhoto photoWithImage:
[imageArrays objectAtIndex:i]]];
}
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
[browser setInitialPageIndex:v.tag];
browser.displayActionButton = YES;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:nc animated:YES];
[nc release];
[browser release];
}
//使imageView同时响应单击与双击事件.
-(void)setImageView:(UIImageView *) imageView{
// UITapGestureRecognizer *recongizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];
// recongizer.delegate = self;
// [imageView addGestureRecognizer:recongizer];
UITapGestureRecognizer *singleTapGR, *doubleTapGR,*mutilTapGR;
singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(selectImage:)];
singleTapGR.delegate = self;
doubleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(showImage:)];
doubleTapGR.numberOfTapsRequired = 2;
doubleTapGR.delegate = self;
mutilTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(log)];
mutilTapGR.numberOfTapsRequired = 3;
mutilTapGR.delegate = self;
//当你需要使用单击时请求双击事件失败.
[singleTapGR requireGestureRecognizerToFail:doubleTapGR];
[doubleTapGR requireGestureRecognizerToFail:mutilTapGR];
[imageView addGestureRecognizer:singleTapGR];
[imageView addGestureRecognizer:doubleTapGR];
[imageView addGestureRecognizer:mutilTapGR];
[singleTapGR release];
[doubleTapGR release];
[mutilTapGR release];
}
-(void)log{
NSLog(@"3");
}
-(void)showImage:(UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)recognizer.view;
if (self.tapCount == 2 ) {
[_photos removeAllObjects];
[_photos addObject:[MWPhoto photoWithImage: imageView.image]];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:nc animated:YES];
[nc release];
[browser release];
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
NSLog(@"tapCount --> %i",touch.tapCount);
self.tapCount = touch.tapCount;
return YES;
}
#pragma mark - MWPhotoBrowserDelegate
-(NSUInteger) numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return [_photos count];
}
- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if (index <[_photos count]) {
return [_photos objectAtIndex:index];
}
return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"scrollView.contentOffset.x-->%f",scrollView.contentOffset.x);
NSLog(@"scrollView.contentOffset.y-->%f",scrollView.contentOffset.y);
}
- (void)viewDidUnload
{
[myScrollView release];
myScrollView = nil;
[userIcon release];
userIcon = nil;
[userBackground release];
userBackground = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[myScrollView release];
[userIcon release];
[userBackground release];
[super dealloc];
}
@end
四
创建自定义的ImagePickerUtil
ImagePickerUtil.h
//
// imagePickerUtil.h
// MyImageViewScroll
//
// Created by skychi on 13-5-13.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "ImageConfirmViewController.h"
@class ImagePickerUitl;
@protocol ImagePickerUitlDelegate
- (void) imagePickerUtil:(ImagePickerUitl *)util
didFinishPickingImage:(UIImage *)image;
@end
@interface ImagePickerUitl : NSObject<UIImagePickerControllerDelegate,UINavigationControllerDelegate,ImageConfirmViewControllerDelegate>{
id<ImagePickerUitlDelegate> delegate;
BOOL _customSize;
}
@property (nonatomic, retain) UIImagePickerController *picker;
@property(nonatomic, assign) id<ImagePickerUitlDelegate> delegate;
@property (nonatomic, assign) CGSize clipSize;
- (void)pickeImageWithDefaultEditWithUIViewController:(UIViewController *) fromVC
inEditMode:(BOOL) editMode
inCaptureMode:(BOOL)captureMode;
- (void)pickeImageWithDefaultEditWithUIViewController:(UIViewController *) fromVC
inCaptureMode:(BOOL)captureMode
Size:(CGSize) size;
@end
相应的ImagePickerUtil.m
//
// imagePickerUtil.m
// MyImageViewScroll
//
// Created by skychi on 13-5-13.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "imagePickerUtil.h"
#import "ImageConfirmViewController.h"
#import "UIImage+Utilities.h"
@implementation ImagePickerUitl
@synthesize delegate;
@synthesize picker;
-(void) pickeImageWithDefaultEditWithUIViewController:(UIViewController *)fromVC inEditMode:(BOOL)editMode inCaptureMode:(BOOL)captureMode{
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
if (captureMode) {
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}else{
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
pickerController.allowsEditing = editMode;
[fromVC presentModalViewController:pickerController animated:YES];
[pickerController release];
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)pickerController didFinishPickingMediaWithInfo:(NSDictionary *)info{
self.picker = pickerController;
UIImage *originalImage = [info valueForKey:UIImagePickerControllerOriginalImage];
UIImage *pickedImage = [info valueForKey:UIImagePickerControllerEditedImage];
if (pickedImage == nil) {
pickedImage = originalImage;
}
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary && picker.allowsEditing==NO) {
ImageConfirmViewController *cv = [[ImageConfirmViewController alloc] initWithNibName:@"ImageConfirmViewController" bundle:[NSBundle mainBundle]];
cv.delegate = self;
[self.picker pushViewController:cv animated:YES];
[cv confirmWithImage:pickedImage];
[cv release];
}else {
[pickerController dismissModalViewControllerAnimated:YES];
if(delegate != nil) {
[delegate imagePickerUtil:self didFinishPickingImage:[pickedImage resizedImageWithMaxSize:CGSizeMake(960, 960) imageOrientation:UIImageOrientationUp]];
}
}
}
-(void)imageConfrim:(UIImage *)image{
if(delegate != nil) {
image = [UIImage fixOrientation :image];
[delegate imagePickerUtil:self didFinishPickingImage:[image resizedImageWithMaxSize:CGSizeMake(960, 960) imageOrientation:UIImageOrientationUp]];
}
}
-(void)pickeImageWithDefaultEditWithUIViewController:(UIViewController *)fromVC inCaptureMode:(BOOL)captureMode Size:(CGSize)size{
}
@end
五.
创建对图片的编辑视图
ImageConfirmViewController.h
//
// ImageConfirmViewController.h
// SimpleDemo
//
// Created by skychi on 13-5-15.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol ImageConfirmViewControllerDelegate <NSObject>
-(void)imageConfrim:(UIImage *)image;
@end
@interface ImageConfirmViewController : UIViewController <UIScrollViewDelegate>{
IBOutlet UIScrollView *_scrollView;
}
@property (nonatomic, assign) id<ImageConfirmViewControllerDelegate> delegate;
@property (nonatomic, retain) UIImage *pickImage;
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
-(void)confirmWithImage:(UIImage *)image;
@end
//
// ImageConfirmViewController.m
// SimpleDemo
//
// Created by skychi on 13-5-15.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "ImageConfirmViewController.h"
@interface ImageConfirmViewController ()
@end
@implementation ImageConfirmViewController
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"select" style:UIBarButtonItemStyleDone target:self action:@selector(confirmPic)];
self.navigationItem.rightBarButtonItem = btn;
// Do any additional setup after loading the view from its nib.
}
-(void)confirmPic{
[delegate imageConfrim:self.pickImage];
[self dismissModalViewControllerAnimated:YES];
}
-(void)confirmWithImage:(UIImage *)image{
self.pickImage = image;
[self.imageView setImage:image];
[_scrollView setContentSize:CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height)];
[_scrollView setMaximumZoomScale:2];
}-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_scrollView release];
[_imageView release];
[super dealloc];
}
- (void)viewDidUnload {
[_scrollView release];
_scrollView = nil;
[self setImageView:nil];
[super viewDidUnload];
}
@end
六,注意相应的outlet及相关委托.