您的位置:首页 > 理论基础 > 计算机网络

【iOS开发-96】网络请求总结,深浅拷贝copy和mutableCopy,SDWebImage图像下载第三方框架

2014-12-23 17:01 633 查看
(1)一般的网络请求数据的处理结构是这样的

NSURL *url=[NSURL URLWithString:nil];
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:5.0f];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//处理数据

//更新主线程
dispatch_async(dispatch_get_main_queue(), ^{
//赋值给数据arr=arrM
});
}];

当然,我们有时候需要缓存一些数据到本地的话,可以这样操作,把数据写到沙盒的cache中,不能写到Document中,会被苹果商店拒绝的:
NSURL *url=[NSURL URLWithString:nil];
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:5.0f];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//处理数据
//方法之一,是把数据写入沙盒,相当于离线数据(官方规定除APP之外产生的文件都不能放在Document中,需放在cache中)
NSString *cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path=[cache stringByAppendingPathComponent:@"friends.plist"];
[data writeToFile:path atomically:YES];
//然后处理数据
NSArray *dictArr=[NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrM=[NSMutableArray array];
for (NSDictionary *dict in dictArr) {
//字典转模型的语句
[arrM addObject:nil];
}
//更新主线程
dispatch_async(dispatch_get_main_queue(), ^{
//赋值给数据arr=arrM
});
}];

(2)copy和mutableCopy总结
——mutableCopy是深拷贝,一听名字就恨厉害的样子,可以把它想象成不管被复制的对象是可变还是不可变,mutableCopy深拷贝之后都会产生一个新对象当然地址也会有所不同。

——copy是浅拷贝,copy性格比较复杂,它喜欢看人下菜,如果被拷贝的对象是不可变的,那么拷贝后地址不变,对象还是那个唯一的对象;如果被拷贝对象是可变的,那么它就和深拷贝一样了,直接新建了个对象,当然地址也不一样。这是因为经过copy拷贝后得到的变量都是imutable的,所以可以说copy时如果原变量不可变,那么它觉得被自己copy后的变量也是不可变的,那么十分安全,于是就直接浅拷贝了一下,地址还是相同的引用同一个对象(反正都不能修改),而如果原变量是可变的,那么经过copy之后的变量是不可变的,所以一个可变一个不可变如果还用相同地址引用同一个对象显然很危险,所以必须是不同地址的不同对象。当然经过copy之后的变量,尽管被定义为Mutable的,但是它仍然是iMutable的。

-(void)test2{
NSMutableString *strM=[NSMutableString stringWithString:@"hello"];
NSMutableString *str1=[strM copy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

-(void)test3{
NSString *strM=@"hello";
NSMutableString *str1=[strM copy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址相同
}

-(void)test4{
NSString *strM=@"hello";
NSString *str1=[strM copy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址相同
}

-(void)test5{
NSMutableString *strM=[NSMutableString stringWithString:@"hello"];
NSString *str1=[strM mutableCopy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

-(void)test6{
NSMutableString *strM=[NSMutableString stringWithString:@"hello"];
NSMutableString *str1=[strM mutableCopy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

-(void)test7{
NSString *strM=@"hello";
NSMutableString *str1=[strM mutableCopy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

-(void)test8{
NSString *strM=@"hello";
NSString *str1=[strM mutableCopy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

-(void)test9{
NSMutableString *strM=[NSMutableString stringWithString:@"hello"];
NSString *str1=[strM copy];
NSLog(@"%@,%@,%p,%p",strM,str1,strM,str1);
//地址不同
}

(3)下载图像
——比如头像,最好是复制给模型里面的变量,这样改变的是模型,如video模型

-(void)loadImg:(NSIndexPath *)indexPath{
//取得对应模型
Video *v=self.videoList[indexPath.row]
[NSURLConnection sendAsynchronousRequest:nil queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
v.image=[UIImage imageWithData:data];
//刷新表格(局部刷新)
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadRowsAtIndexPath:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
}];
}
//然后在tableView的数据方法cell的赋值方法中赋值即可,
if(!v.image){
cell.imageView.image=[UIImage imageNamed:@"default"];
}else{
cell.imageView.image=v.image;
}

(4)第三方框架来进行图像下载:SDWebImage,下载地址:https://github.com/rs/SDWebImage/
——导入第三方框架后,第一件事情就是CMD+B进行编译,以防部分框架需要依赖其他框架才能运行。一般编译都会出现很多警告,正常。

——大部分人都只用了UIImageView+WebCache这个分类来处理图片

以上(3)中的代码可以修改成,直接在cell赋值方法中,这个方法就是框架提供的方法:

if(v.image){
cell.imageView.image=v.image;
}else{
[cell.imageView setImageWithURL:url placeholderImage:self.placeHolderImg options:0 completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType){
v.image=image;//记得给模型赋值
}];
}

备注:

(1)继承的子类中,如果需要使用成员变量(_name之类的),需要合成一下,@synthesize name=_name;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐