在一次練習中用到的 很簡單 供學習 封裝好了這樣的類SSImageView (繼承UIview的類) 可直接使用(類方法實現(xiàn)) 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); //需要回調(diào)方法或者檢驗是否存入成功: 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]; }
|
|
來自: 求知665 > 《ios有用網(wǎng)頁》