iOS 技术浅谈之第四篇 (开发框架)

在iOS开发中不可避免的会用到一些第三方类库,它们提供了很多实用的功能,使我们的开发变得更有效率;同时,也可以从它们的源代码中学习到很多有用的东西。下面介绍几个我开发中常用到的第三方类库,和大家一起学习下。

网络请求

GitHub下载地址:https://github.com/AFNetworking/AFNetworking

AFNetworking官网地址:

三步导入AFN框架的步骤

1.将框架程序拖拽进项目

2.添加iOS框架引用

SystemConfiguration.framework

MobileCoreServices.framework

3.修改xxx-Prefix.pch文件

#import<MobileCoreServices/MobileCoreServices.h>

#import<SystemConfiguration/SystemConfiguration.h>

AFN更详细的使用方法见:

当然还有一个更强大的网络框架ASHttpRequest(ASI),只是ASI的开发者已经没有对它进行更新了,在iOS7和iOS8的兼容性存在一些问题,所以现在在使用AFN。下面提供ASI的下载和使用:

GitHub下载地址:https://github.com/pokeb/asi-http-request

API使用参考:

二、SDWebImage 图片异步加载及缓存

GItHub下载地址:https://github.com/rs/SDWebImage

GItHub下载地址:https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage

SDWebImage用于异步下载网络上的图片,并支持对图片的缓存等。多数情况下是使用UIImageView+WebCache为UIImageView异步加载图片:

#import <SDWebImage/UIImageView+WebCache.h>// …[cell.imageView setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 需要注意的是,,pladeholderImage 的大小一定要大于UIImageView的大小,否则可能不显示placeholderImage图片。

它还支持block语法用于在加载完成时做一些操作:

[cell.imageView setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"placeholder.png"]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {… completion code here …}];SDWebImage并不局限于UIImageView上,使用SDWebImageManager完成更多的操作:SDWebImageManager *manager = [SDWebImageManager sharedManager];[manager downloadWithURL:imageURLoptions:0progress:^(NSUInteger receivedSize, long long expectedSize){// 下载进度}completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){if (image) {// 下载完成}}]; 或者使用Image Downloader也是一样的效果:[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURLoptions:0progress:^(NSUInteger receivedSize, long long expectedSize){// 进度} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {if (image && finished) {// 下载完成}}];UIActivityIndicator-for-SDWebImage 为SDWebImage显示加载效果,用于为SDWebImage在UIImageView加载图片时,显示加载效果(UIActivityIndicatorView实现),它提供以下方法:

– (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle; 更详细的使用见:

如果你只是匆匆忙忙下载了一个framework 就回到了自己工程,可能会遇到无法使用的情况:那么下面的文章来解决你遇到的问题:

三、JSONKit 数据解析

GItHub下载地址:https://github.com/johnezang/JSONKit

JSONKit提供比SBJson更优异的性能以及更加简便的使用方法,但是中文最好使用utf-8格式(/uXXXX),否则容易造成乱码。

人生如果错了方向,停止就是进步”。

iOS 技术浅谈之第四篇 (开发框架)

相关文章:

你感兴趣的文章:

标签云: