图片相关 UIActionSheet

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;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值