NSString,NSData,NSURL,UIImage……互转
技巧:想要获得什么即以什么开头+With
1、NSString ——> NSURL 将字符串地址封装成可用的URL类型
NSURL *url = [NSURL URLWithString:str];
2、NSURL ——> NSData
NSData *data = [NSData dataWithContentsOfURL:url];
3、NSData ——> UIImage
UIImage *image = [UIImage imageWithData:data];
4、UIImage ——> NSData
NSData *dataImagePng = UIImagePNGRepresentation(image);
NSData *dataImageJpg = UIImageJPEGRepresentation(image, 1);(第二个参数表示压缩的程度)
5、NSString ——> UIImage
UIImage *dataImageFile = [UIImage imageWithContentsOfFile:@""];
6、NSString ——> NSData
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
7、NSData ——> NSString
NSString *strdata = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
8、NSURL ——> NSString
NSString * str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
9、NSData ——> NSDictionary
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];