第三方框架的使用

框架的使用

1.AFNetworking :是用来请求网络的

  AFHTTPRequestOperationManager *mangr = [AFHTTPRequestOperationManager manager];

    NSMutableDictionary *paramas = [NSMutableDictionary dictionary];

    paramas[@"access_token"] = [MDAccountTool account].access_token;

    //发送Get 请求

    [mangr GET:<#(NSString *)#> parameters:<#(id)#> success:^(AFHTTPRequestOperation *operation, id responseObject) {     

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];

********************************************************************************************************************

2.MBProgressHUD2:这是缓冲时的涡轮效果

- (void)webViewDidStartLoad:(UIWebView *)webView

{

//旋转的效果

    [MBProgressHUD showMessage:@"正在加载..."];

}

//web加载完成的时候调用

- (void)webViewDidFinishLoad:(UIWebView *)webView

{//取消旋转

    [MBProgressHUD hideHUD];

 

}

*********************************************************************************************************

3.MJExtension:这是字典属性转换成模型的时候调用,模型转字典,还有归档和解档 

        NSArray *statusArray = responseObject[@"statuses"];

                //字典数组转MDStatus模型数组

        self.statuses = (NSMutableArray *)[MDStatus objectArrayWithKeyValuesArray:statusArray];

             //刷新表格数据

        [self.tableView reloadData];

        //等价与上面的一行代码

//        for (NSDictionary *dict in statusArray) {

//            //字典转MDStatus,数组中字典是转不了模型

//            MDStatus *sta = [MDStatus objectWithKeyValues:dict];

//            if (sta.pic_urls.count) {

//                NSLog(@"%@",sta.pic_urls[0]);

//            }

//            [self.statuses addObject:sta];

//            

//        }

********************************************************************************************************************

4.SDWebImage:加载网络图片的时候调用

SDWebImage 的好处:

1.属于异步请求,不会阻塞主线程(界面)

2.自动创建缓存

3.可以让图片循环利用

注意点:因为自动创建缓存,很容易造成内存的警告

解决的方法:

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application

{

   //停止所有的图片下载

    [[SDWebImageManager sharedManagercancelAll];

    

    //清空所有缓存

    [[SDWebImageManager sharedManager].imageCache clearMemory];

 

 

}

 //设置图片

 

    [cell.imageView sd_setImageWithURL:status.user.profile_image_url placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];

归档和解档

//底层就是把当前的类的属性,一一归档和解档

 

MJCodingImplementation;

***************************************************************************************

5.MJRefresh:下拉刷新的效果

//自动下拉刷新

    [self.tableView headerBeginRefreshing];

 

***************************************************************************************

6.NSDate+MJ 判断时间与当前时间的比较

//设置时间格式(由于使用点语法获取数据的,所以要重新设置Get方法)

-(NSString *)created_at

{

    // Sat Mar 26 16:05:03 +0800 2016 微博给我们的时间格式

    //1.先要把字符串转化成 NSDate

    //转换 EEE MMM d HH:mm:ss Z yyyy

    //日期格式转换

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"EEE MMM d HH:mm:ss Z yyyy “;

//记得格式本地化,是系统知道时间是那个国家的语言(一定要设置,不然不会显示出时间)

 

    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_us"];

    NSDate *creat_time = [formatter dateFromString:_created_at];

    if ([creat_time isThisYear]) {//今年

        //两天前

        if ([creat_time isToday]) {//今天

            NSDateComponents *cmp = [creat_time deltaWithNow];

            if (cmp.hour >= 1) {

                return [NSString stringWithFormat:@"%ld小时前",cmp.hour];

            }else if(cmp.minute > 1)

            {

                return [NSString stringWithFormat:@"%ld分钟前",cmp.minute];

            }else

            {

                return @"刚刚";

            }

            

        }else if ([creat_time isYesterday])

        {//昨天

            formatter.dateFormat = @"昨天 HH:mm";

            return [formatter stringFromDate:creat_time];

        

        }else

        {//前天

            formatter.dateFormat = @"MM-dd HH:mm";

            return [formatter stringFromDate:creat_time];

        }

        

   

    }else{//不是今年

         formatter.dateFormat = @"yyyy-MM-dd HH:mm";

        return [formatter stringFromDate:creat_time];

    }

    

    return _created_at;

 

}

 

***************************************************************************************

MJPhotoBrowser:图片浏览器

//添加九个配图控件(要把传过来,利用属性传值)

-(void)setUpAllChildVC

{

    //九宫格中

    for (int i = 0; i < 9 ; i++) {

        UIImageView *imageView = [UIImageView alloc].init;

        /**

         * UIViewContentModeScaleToFill :将整个图片压缩到适合控件的大小

         UIViewContentModeScaleAspectFit :按比例缩放到不超出边框

         UIViewContentModeScaleAspectFill:按比例缩放到到一边(宽或高)不超出边界合适就不在改变

        UIViewContentModeCenterview 与图片的中心点对齐,不会居中

         *

         */

        //添加点图片手势按钮

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];

        [imageView addGestureRecognizer:tap];

        imageView.tag = i;

        //当使用UIImageView的时候,记得设置图片的交互

        imageView.userInteractionEnabled = YES;

        imageView.contentMode = UIViewContentModeScaleAspectFill;

        //把超出边框的图片去掉

        imageView.clipsToBounds = YES;

        [self addSubview:imageView];

    }

 

}

//添加点图片手势按钮

-(void)tapGesture:(UITapGestureRecognizer *)tap

{

     //在手势的图片添加Tag,这可以知道获取的是第几张图片(通过手势可以获取点击的图片)

    UIImageView *imageV = (UIImageView *) tap.view;

    //点击图片的时候调用

    //创建图片浏览器

    int i = 0;

    NSMutableArray *array = [NSMutableArray array];

    for (MDPhotos *photos in _pic_urls) {

        //MDphotos 转换成 MJphoto

        //MJPhoto.h 的文件中 ,要实现的属性有

        //1.@property (nonatomic, strong) NSURL *url;

        //2.@property (nonatomic, strong) UIImageView *srcImageView; // 来源view(你点击那个图片)

        //3.@property (nonatomic, assign) int index; // 索引

        MJPhoto *mjPhotos = [[MJPhoto alloc] init];

        //图片不清晰时,可以键连接的改一下

        NSString *urlString = photos.thumbnail_pic.absoluteString;

        urlString = [urlString stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];

        mjPhotos.url = [NSURL URLWithString:urlString];

//        NSLog(@"%@",photos.thumbnail_pic);

        mjPhotos.index = i ;

        mjPhotos.srcImageView = imageV;

        [array addObject:mjPhotos];

        i ++ ;

    }

    

    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];

    /*MJ头文件提示要实现的,以后使用框架的时候,要看头文件

     // 所有的图片对象(MJPhoto)

     @property (nonatomic, strong) NSArray *photos;

     // 当前展示的图片索引

     @property (nonatomic, assign) NSUInteger currentPhotoIndex;

     */

    browser.photos = array;//所有图片对象

    browser.currentPhotoIndex = imageV.tag ;

    //以后看到要显示到主窗口的 可以 考虑框架是否有 show(看框架头文件)

    [browser show];

    

 

 

 

}

原文地址:https://www.cnblogs.com/meixian/p/5370946.html