ViewController.m
#import "ViewController.h"
// 不要忘记签订协议
@interface ViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation ViewController
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
1.创建一个图片视图.
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
self.imageView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.imageView];
[_imageView release];
2.创建一个动作框.
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选择", nil];
[sheet showInView:self.view];
[sheet release];
3.开启用户交互.
self.imageView.userInteractionEnabled = YES;
4.创建长按手势.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[self.imageView addGestureRecognizer:longPress];
[longPress release];
}
5.实现长按手势的方法.
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
6.让图片保存到本地. UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(imageToPhotosAlbum:Error:contextinfo:), nil);
}
}
7.实现方法.
- (void)imageToPhotosAlbum:(UIImage *)image Error:(NSError *)error contextinfo:(NSString *)info {
if (!error) {
NSLog(@"保存成功");
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];
8.设置图片使其可编辑.
imagePC.allowsEditing = YES;
9.要通过choose获取本地的图片就必须签订协议.
imagePC.delegate = self;
10.模态显示出来.
[self presentViewController:imagePC animated:YES completion:^{
}];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
11.先让相册消失.
[picker dismissViewControllerAnimated:YES completion:nil];
// 控制原始图像.
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.imageView.image = image;
}
编辑并保存手机中的图片至本地
最新推荐文章于 2024-03-19 18:14:11 发布