IOS 单击图片全屏显示并存储到本地相册

在一次练习中用到的  很简单   供学习  封装好了这样的类SSImageView (继承UIview的类) 可直接使用(类方法实现)

H文件

#import <UIKit/UIKit.h>


@interface SSImageView :UIView<UIScrollViewDelegate>{

   UIImageView * _imageView;

   UIScrollView * _contentView;

   UIButton * _collectionButton;


}

- (void) showImage;


- (void) hideImage;


- (void) setImage:(UIImage *) image;


+ (void) viewWithImage:(UIImage *) image;



@end



M文件

#import "SSImageView.h"

#import "AppDelegate.h"




@interface SSImageView ()

//全屏显示时图片的size

- (CGSize) preferredSize:(UIImage *)image;


@end


@implementation SSImageView

- (void)dealloc

{

    _contentView =nil;

    _imageView = nil;

    [superdealloc];

}


- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        self.backgroundColor = [UIColorclearColor];

        

        _contentView=[[UIScrollViewalloc]initWithFrame:frame];

        _contentView.backgroundColor = [UIColorwhiteColor];

        _contentView.delegate=self;

        _contentView.bouncesZoom=YES;

        

        _contentView.minimumZoomScale = 0.5;

        _contentView.maximumZoomScale = 5.0;

        

        _contentView.showsHorizontalScrollIndicator=NO;

        _contentView.showsVerticalScrollIndicator=NO;

        [selfaddSubview:_contentView];

        

        

        _imageView = [[UIImageViewalloc]initWithFrame:CGRectZero];

        _imageView.userInteractionEnabled =YES;

        [_contentViewaddSubview:_imageView];

        

        //为图片添加手势

        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(hide)];

        tapGesture.numberOfTapsRequired = 1;

        tapGesture.enabled =YES;

        [tapGesturedelaysTouchesBegan];

        [tapGesturecancelsTouchesInView];

        [_imageViewaddGestureRecognizer:tapGesture];

        

        _collectionButton = [UIButtonbuttonWithType:(UIButtonTypeSystem)];

       _collectionButton.frame =CGRectMake(frame.origin.x + frame.size.width  - 70, frame.origin.y + frame.size.height - 50, 60, 40);

        [_collectionButtonsetTitle:@"保存"forState:(UIControlStateNormal)];

        [selfaddSubview:_collectionButton];

        [_collectionButtonaddTarget:selfaction:@selector(didClickCollectionButtonAction:)forControlEvents:(UIControlEventTouchDown)];

       //为视图增加边框

        _collectionButton.layer.masksToBounds=YES;

        _collectionButton.layer.cornerRadius = 5.0;

        _collectionButton.layer.borderWidth=1.0;

        _collectionButton.layer.borderColor=[[UIColor lightGrayColor] CGColor];

        [_collectionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];



        

    }

    return self;


}


- (void)didClickCollectionButtonAction:(UIButton *)button{

    //直接存入本地相册可以用:

    UIImageWriteToSavedPhotosAlbum(_imageView.image,nil,nil, nil);

    //需要回调方法或者检验是否存入成功:

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

}

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

{

    

   if (error !=NULL) {

        UIAlertView *photoSave = [[UIAlertViewalloc]initWithTitle:nilmessage:[NSStringstringWithFormat:@"%@",error]delegate:nilcancelButtonTitle:nilotherButtonTitles:nil];

        [photoSaveshow];

        [photoSave dismissWithClickedButtonIndex:0animated:YES];

        [photoSaverelease];

        photoSave =nil;

    }else {

        UIAlertView *photoSave = [[UIAlertViewalloc]initWithTitle:@"\n\n保存成功" message:nildelegate:nilcancelButtonTitle:nilotherButtonTitles:nil];

        [photoSaveshow];

        [photoSave dismissWithClickedButtonIndex:0animated:YES];

        [photoSaverelease];

        photoSave =nil;

    }

}


- (void) setImage:(UIImage *) image {

   CGSize size = [selfpreferredSize:image];

    _imageView.frame =CGRectMake(0, 0, size.width, size.height);

    

    _contentView.contentSize= size;

    

    _imageView.center =self.center;

    _imageView.image = image;

}


- (CGSize) preferredSize:(UIImage *) image {

    

   CGFloat width = 0.0, height = 0.0;

   CGFloat rat0 = image.size.width / image.size.height;

    CGFloat rat1 =self.frame.size.width /self.frame.size.height;

   if (rat0 > rat1) {

        width =self.frame.size.width;

        height = width / rat0;

    }else {

        height =self.frame.size.height;

        width = height * rat0;

    }

    

   returnCGSizeMake(width, height);

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return_imageView;

}


-(void) scrollViewDidZoom:(UIScrollView *)scrollView{

   CGFloat x = scrollView.center.x,y = scrollView.center.y;

    x = scrollView.contentSize.width > scrollView.frame.size.width?scrollView.contentSize.width/2 :x;

    y = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height/2 : y;

    _imageView.center =CGPointMake(x, y);

}


- (void) showImage {

    

    _contentView.transform =CGAffineTransformMakeScale(0.1, 0.1);

    _contentView.alpha = 0;

    _collectionButton.alpha = 0;

    

    [UIView animateWithDuration:0.5 animations:^{

        _contentView.alpha = 1.0;

        _collectionButton.alpha = 1.0;

        _contentView.transform =CGAffineTransformMakeScale(1, 1);

        _contentView.center=self.center;

    }];

    

}


-  (void) hideImage {

    [UIView animateWithDuration:0.25 animations:^{

        _contentView.transform =CGAffineTransformMakeScale(0.1, 0.1);

        _contentView.alpha = 0;

        _collectionButton.alpha = 0;

    }completion:^(BOOL finished) {

       if (finished) {

            _contentView.alpha=1;

            [_contentViewremoveFromSuperview];

            [_imageViewremoveFromSuperview];

            [selfremoveFromSuperview];

        }

    }];

}


+ (void) viewWithImage:(UIImage *) image {


    AppDelegate * delegate = ((AppDelegate *)[UIApplicationsharedApplication].delegate);

   UIWindow * window = delegate.window;

    SSImageView * imageViewer = [[SSImageViewalloc]initWithFrame:window.frame];

    [imageViewersetImage:image];

    

    [windowaddSubview:imageViewer];

    [imageViewershowImage];

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值