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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值