ViewController.m
#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
@interface ViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,retain)UIImageView *myImage;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.myImage=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
self.myImage.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.myImage];
[_myImage release];
[self.myImage sd_setImageWithURL:[NSURL URLWithString:@"http://v.juhe.cn/movie/picurl?2583246"]];
// 创建一个UIActionSheet
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取", nil];
[sheet showInView:self.view];
self.myImage.userInteractionEnabled=YES;
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.myImage addGestureRecognizer:longPress];
[longPress release];
}
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"%@",@"lalala");
// 判断手势当前的状态,只有刚开始会保存图片
if (longPress.state==UIGestureRecognizerStateBegan) {
// 把图片写到本地相册中
UIImageWriteToSavedPhotosAlbum(self.myImage.image, nil, nil, nil);
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
if (buttonIndex==0) {
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
// 设置代理人
// 这个地方很特殊,这个代理人执行两套协议,需要签订两个协议
picker.delegate=self;
// 允许进行编辑
picker.allowsEditing=YES;
// 用模态让他显示出来
[self presentViewController:picker animated:YES completion:^{
}];
}
}
#pragma mark 点击choose之后,可以选中当前的图片,并且返回让图片显示在imageView上
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 隐藏的效果就取消了,所以,我们需要手动添加dismiss的方法,让相册消失
[self dismissViewControllerAnimated:YES completion:^{
}];
NSLog(@"%@",info);
// 从返回过来的字典对象info中提取image
UIImage *image=info[UIImagePickerControllerOriginalImage];
self.myImage.image=image;
}