首页 > 代码库 > UIImageView加载网络图片,NSOperationQueue来异步加载图片

UIImageView加载网络图片,NSOperationQueue来异步加载图片

#import "newDetailViewController.h"
#import "Common.h"
#import "newObject.h"
#import "NetRequest.h"
#import <UIKit/UIKitDefines.h>


@interface newDetailViewController ()<UIWebViewDelegate>
{
    UILabel *GWDetailTitleText;
    UILabel *GWDetailTitleText1;
    UITextView *GWDetailContentText;
    
    UIImageView *imageview;
    NSURL *url;
    
    UIView *opaqueview;
    UIActivityIndicatorView *activityIndicator;
    
}
@end

@implementation newDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            self.view.bounds = CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height );
        }
    }
    return self;
}

- (void)viewDidLoad
{
    
     [UIApplication sharedApplication].statusBarHidden = YES;
    [super viewDidLoad];
    //[self initView];
}

-(void)initView
{
    
    UIButton *loginButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 40, 0, 40, 30)];
    [loginButton setTitle:@"返回" forState:UIControlStateNormal];
    loginButton.titleLabel.textColor =[UIColor blackColor];
    [loginButton addTarget:self action:@selector(backtofather) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    GWDetailTitleText = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH - 20, 40)];
    GWDetailTitleText.textAlignment = NSTextAlignmentCenter;
    GWDetailTitleText.numberOfLines = 0;
    GWDetailTitleText.lineBreakMode = UILineBreakModeCharacterWrap;
    [self.view addSubview:GWDetailTitleText];
    
    
    
    GWDetailTitleText1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH - 20, 20)];
    GWDetailTitleText1.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:GWDetailTitleText1];

    
    
    web = [[UIWebView alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH - 20, self.view.bounds.origin.y + self.view.bounds.size.height - 110)];
    web.scalesPageToFit = YES;
    web.delegate = self;
    [[web layer] setCornerRadius:10];
    [web setClipsToBounds:YES];
    web.scalesPageToFit = NO;
    for (id subview in web.subviews)
    {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        {
            ((UIScrollView *)subview).bounces = NO;
        }
    }
    web.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:web];

    
    
    
//    GWDetailContentText = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH - 20, self.view.bounds.origin.y + self.view.bounds.size.height - 110)];
//    GWDetailContentText.layer.borderWidth = 1;
//    GWDetailContentText.editable = NO;
//    [self.view addSubview:GWDetailContentText];


}


-(void)setchoicedDate:(newObject *)myarray
{
    [self initView];
     MyLog(@"%@  ", myarray );
    //MyLog(@"%@", myarray[0]);//[@"rel_title"]
    GWDetailTitleText.text = myarray.rel_title;
    
    NSString *date =[[NSString alloc]initWithString:myarray.rel_date];
    date = [date substringToIndex:10];
    NSString *mystring = [NSString stringWithFormat:@"发布人:%@ 日期:%@",myarray.rel_user,date];
    GWDetailTitleText1.text = mystring;
    MyLog(@"%@",myarray.rel_picpath);
    if (![myarray.rel_picpath isEqualToString:@"null"]) {
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"http://www.xf0573.com",myarray.rel_picpath]];
        imageview = [[UIImageView alloc]initWithFrame:CGRectMake(80, 0, 160, 120)];
        imageview.layer.masksToBounds = YES;
        imageview.layer.cornerRadius = 5.0f;
        [imageview setBackgroundColor:[UIColor grayColor]];
        imageview.image = [UIImage imageNamed:@"moren.png"];
        [web addSubview:imageview];
        
        opaqueview = [[ UIView   alloc]  initWithFrame: CGRectMake(0, 0,  160, 120)];
        activityIndicator  = [[UIActivityIndicatorView   alloc]  initWithFrame: CGRectMake( 0, 0,  160, 120)];
        [ opaqueview setBackgroundColor:[UIColor blackColor]];
        [ opaqueview setAlpha:0.6 ];
        [ imageview addSubview: opaqueview];
        [ opaqueview addSubview: activityIndicator];
        [ activityIndicator startAnimating ];
        opaqueview.hidden  = NO ;
        
        web1 = [[UIWebView alloc]initWithFrame:CGRectMake(10, 120, SCREEN_WIDTH - 20, self.view.bounds.origin.y + self.view.bounds.size.height - web.frame.origin.y - 130)];
        web1.scalesPageToFit = YES;
        web1.delegate = self;
        [[web1 layer] setCornerRadius:10];
        [web1 setClipsToBounds:YES];
        web1.scalesPageToFit = NO;
        for (id subview in web1.subviews)
        {
            if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            {
                ((UIScrollView *)subview).bounces = NO;
            }
        }
        web1.backgroundColor = [UIColor whiteColor];
        [web1 loadHTMLString:myarray.rel_content baseURL:nil];
        [web addSubview:web1];
        
        
        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
        NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
        [operationQueue addOperation:op];
        
        
        
        //[web loadRequest:req];
    }else if ([myarray.rel_picpath isEqualToString:@"null"])
    {
    
    [web loadHTMLString:myarray.rel_content baseURL:nil];
    //GWDetailContentText.text = myarray.rel_content;
    }

}
- (void)downloadImage
{
    
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    imageview.image = image;
    
    [activityIndicator stopAnimating];
    opaqueview.hidden  = YES ;
}

-(void)backtofather
{
    [self dismissModalViewControllerAnimated:YES];
}
@end

UIImageView加载网络图片,NSOperationQueue来异步加载图片