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


转载于:https://my.oschina.net/yyyyyyyyqs/blog/548458

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值