app添加启动图片封装的方法

//
//  ViewController.m
//  启动图方法抽取
//
//  Created by 小明 on 15/12/2.
//  Copyright © 2015年 hjb. All rights reserved.
//
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#import "UIImageView+WebCache.h"

#import "ViewController.h"

@interface ViewController ()  //

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //启动图片借口数组
    NSArray* arr = @[ @"http://image.tianjimedia.com/uploadImages/2015/292/12/8T7C2IQF97S4_680x500.jpg", @"http://img6.faloo.com/Picture/0x0/0/747/747488.jpg", @"http://image.tianjimedia.com/uploadImages/2015/292/40/3W15Y23LSYG1_680x500.jpg", @"http://image.tianjimedia.com/uploadImages/2015/292/14/U7S7FY1B3173_680x500.jpg", @"http://image.tianjimedia.com/uploadImages/2015/292/36/236NY83414Z6_680x500.jpg" ];
    //调用‘设置启动图片’方法,一句话搞定!第一次从本地加载图片,之后是网络图片。
    //想要第一次就从网络上取图片,sdwebimage的方法自己挑一个~
    [self uploadGuideImageWithArray:arr];//封装好的方法
}



/**
 *  设置启动图片
 *
 *  @param arr 请求图片的借口数组
 */
- (void)uploadGuideImageWithArray:(NSArray*)arr
{

    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    //UIScrollView分页
    scrollView.pagingEnabled = YES;
    //隐藏滑动条
    scrollView.showsHorizontalScrollIndicator = NO;
    //设置内置约束
    scrollView.contentSize = CGSizeMake(arr.count * kScreenWidth, kScreenHeight);

    for (NSInteger i = 0; i < arr.count; i++) {

        NSString* urlStr = [NSString stringWithFormat:@"%@", arr[i]];

        NSURL* url = [NSURL URLWithString:urlStr];
        //调用存储方法
        [self uploadGuideImageWithUrl:url imageName:[NSString stringWithFormat:@"%@", arr[i]]];
        //获取缓存图片
        UIImage* im = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[NSString stringWithFormat:@"%@", arr[i]]];
        //图片视图
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth, kScreenHeight)];
        imageView.image = im;
        [scrollView addSubview:imageView];
    }

    [self.view addSubview:scrollView];
}
/**
 *  网络请求图片,并用SDWebimage缓存图片
 *
 *  @return void
 */
- (void)uploadGuideImageWithUrl:(NSURL*)imageURL imageName:(NSString*)cachename
{
    //SDWebimage请求图片
    //    UITableView
    SDWebImageDownloader* downloader = [SDWebImageDownloader sharedDownloader];
    [downloader downloadImageWithURL:imageURL
                             options:0
                            progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                // progression tracking code
                            }
                           completed:^(UIImage* image, NSData* data, NSError* error, BOOL finished) {
                               if (image && finished) {
                                   // SDWebimage存储图片
                                   [[SDImageCache sharedImageCache] storeImage:image forKey:[NSString stringWithFormat:@"%@", cachename]];
                               }
                           }];
}
@end


你可能感兴趣的:(app添加启动图片封装的方法)