ios 点击放大图片,保存至手机相册

本文介绍了一个用于iOS应用中的图片放大展示与保存功能的实现方法。通过UIKit框架下的UIImageView类扩展,实现了图片的平滑放大及全屏显示,并提供了一个保存按钮以便用户将图片保存到相册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接贴.m文件代码

#import "UIImageView+Scale.h"

static CGRect oldframe;

 

@implementation UIImageView (Scale)

//放大

-(void)showImageView:(UIImageView *)targetImgView {

    UIImage *image = targetImgView.image;

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    oldframe =[targetImgView convertRect:targetImgView.bounds toView:window];

    bgView.backgroundColor = [UIColor lightGrayColor];

    bgView.alpha = 0;

    UIImageView *imageView = [[UIImageView alloc ] initWithFrame:oldframe];

    imageView.image = image;

    imageView.tag = 1001;

    [bgView addSubview:imageView];

    [window addSubview:bgView];

    bgView.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImage:)];

    [bgView addGestureRecognizer:tap];

//如果图片还没有加载出来会卡在这里

    if (image) {

        [UIView animateWithDuration:0.3 animations:^{

            imageView.frame = CGRectMake(0,  (SCREEN_HEIGHT - image.size.height * SCREEN_WIDTH / image.size.width)/2, SCREEN_WIDTH, image.size.height * SCREEN_WIDTH/image.size.width);

            bgView.alpha = 1;

        }];

    }

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(bgView.right - 80, imageView.bottom + 10, 80, 30)];

    [btn setTitle:@"保存" forState:0];

    [bgView addSubview:btn];

    [btn addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];

}

//缩小隐藏

- (void)hideImage:(UITapGestureRecognizer *)tap {

    UIView *backgroundView=tap.view;

    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1001];

    [UIView animateWithDuration:0.5 animations:^{

        imageView.frame = oldframe;

        backgroundView.alpha=0;

    } completion:^(BOOL finished) {

        [backgroundView removeAllSubviews];

        [backgroundView removeFromSuperview];

    }];

 

}

//保存

- (void)saveImage:(UIButton *)sender {

    UIImageView *imageView = (UIImageView *)[sender.superview viewWithTag:1001];

    UIImage *image = imageView.image;

    UIImageWriteToSavedPhotosAlbum(image,self,@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);

}

//保存回调

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    NSString *message;

    if (!error) {

        message = @"成功保存到相册~";

    }else {

        message = [error description];

    }

    NSLog(@"message is %@",message);

    [MBProgressHUD showHUD:message];

}

@end

 

转载于:https://www.cnblogs.com/wangshankun/p/6269148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值