// // ViewController.m // ImageDownShow // // Created by KEN on 15/7/23. // Copyright (c) 2015年 Tin. All rights reserved. // #import "ViewController.h" #import "ImageViewController.h" @interface ViewController () @end @implementation ViewController -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if( [ segue.destinationViewController isKindOfClass:[ImageViewController class] ]) { ImageViewController * ivc = ( ImageViewController *) segue.destinationViewController ; NSString * str = nil ; if( [segue.identifier isEqualToString:@"num1"]) { str = @"http://f0.topit.me/0/f8/68/1122299417ae868f80o.jpg" ; }else { str = @"http://d.hiphotos.baidu.com/zhidao/pic/item/f3d3572c11dfa9ecd1eaad8762d0f703908fc15e.jpg" ; } ivc.imageURL = [ NSURL URLWithString: str] ; ivc.title = segue.identifier ; } NSLog(@"%.2f" , 1.24444 ); } @end
#import <UIKit/UIKit.h> @interface ImageViewController : UIViewController @property ( nonatomic , strong) NSURL * imageURL ; @end
// // ImageViewController.m // ImageDownShow // // Created by KEN on 15/7/23. // Copyright (c) 2015年 Tin. All rights reserved. // #import "ImageViewController.h" @interface ImageViewController () <UIScrollViewDelegate> @property ( nonatomic , strong) UIImageView * imageView ; @property ( nonatomic , strong) UIImage * image ; @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator; @end @implementation ImageViewController -(void) setImageURL:(NSURL *)imageURL { _imageURL = imageURL ; // self.image = [ UIImage imageWithData:[NSData dataWithContentsOfURL:self.imageURL]] ; [self startDownloadingImage] ; } -(void) startDownloadingImage { self.image = nil ; if( self.imageURL){ NSURLRequest * request = [ NSURLRequest requestWithURL:self.imageURL] ; NSURLSessionConfiguration * configuration = [ NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession * session = [ NSURLSession sessionWithConfiguration:configuration] ; NSURLSessionDownloadTask * task = [ session downloadTaskWithRequest:request completionHandler:^(NSURL *localFile, NSURLResponse *response, NSError *error) { if(!error) { if( [request.URL isEqual:self.imageURL]) { UIImage * image =[ UIImage imageWithData:[NSData dataWithContentsOfURL:localFile]] ; dispatch_async(dispatch_get_main_queue() , ^{ self.image = image ; }); } } }] ; [task resume]; } } -(UIView * ) viewForZoomingInScrollView:(UIScrollView *)scrollView { return self.imageView ; } -(void) viewDidLoad { [super viewDidLoad]; [self.scrollView addSubview:self.imageView] ; [self.indicator startAnimating]; } -( UIImageView * ) imageView { if( !_imageView) _imageView = [[ UIImageView alloc] init ] ; return _imageView ; } -( UIImage * ) image { return self.imageView.image ; } -(void) setImage:(UIImage *)image { self.imageView.image = image ; [self.imageView sizeToFit]; self.scrollView.contentSize = self.image? self.image.size : CGSizeZero; [self.indicator stopAnimating]; } -(void) setScrollView:(UIScrollView *)scrollView { _scrollView = scrollView ; _scrollView.minimumZoomScale = 0.2 ; _scrollView.maximumZoomScale = 2 ; _scrollView.delegate = self ; self.scrollView.contentSize = self.image? self.image.size : CGSizeZero; } @end
时间: 2024-11-06 07:22:13