转载自:http://www.cnblogs.com/duchengdong/archive/2012/03/29/2422997.html
简介:
1. NSData 与 NSString
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
//将data类型的数据,转成UTF8的数据
+(NSString *)dataToUTF8String:(NSData *)data
{
NSString *buf = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [buf autorelease];
}
//将string转换为指定编码
+(NSString *)changeDataToEncodinString:(NSData *)data encodin:(NSStringEncoding )encodin{
NSString *buf = [[[NSString alloc] initWithData:data encoding:encodin] autorelease];
return buf;
}
2. NSData 与 UIImage
NSData->UIImage
UIImage *aimage = [UIImage imageWithData: imageData];
//例:从本地文件沙盒中取图片并转换为NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage *aimage = [UIImage imageWithData: imageData];
3.NSData与NSArray NSDictionary
+(NSString *)getLocalFilePath:(NSString *) fileName
{
return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@“Documents”,fileName];
}
包括将NSData写进Documents目录
从Documents目录读取数据
在进行网络数据通信的时候,经常会遇到NSData类型的数据。在该数据是dictionary结构的情况下,系统没有提供现成的转换成NSDictionary的方法,为此可以通过Category对NSDictionary进行扩展,以支持从NSData到NSDictionary的转换。声明和实现如下:
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data {
CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL);
if(list == nil) return nil;
if ([(id)list isKindOfClass:[NSDictionary class]]) {
return [(NSDictionary *)list autorelease];
}
else {
CFRelease(list);
return nil;
}
}
NSDate
//函数作用 :date根据formatter转换成string
+(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return[dateFormatter stringFromDate:date];
}
//函数作用 :string类型的时间戳转换成时间
+(NSString*)dateStringToString:(NSString *)dateStr
{
if ([dateStr isKindOfClass:[NSNull class]]) {
return nil;
}
else{
NSInteger time = [dateStr intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];
return needtime;
}
}
//函数作用 :将日期从原格式转换成需要的格式
+(NSString*)convertDateFormatter:(NSString*)sourceFormatter
targetFormatter:(NSString*)targetFormatter
dateString:(NSString*)dateString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:sourceFormatter];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:targetFormatter];
return[dateFormatter stringFromDate:date];
}
//函数作用 :将日期字符串转换成date
+(NSDate *)stringToDate:(NSString *)formatter dateString:(NSString *)dateString{
NSDateFormatter *dateFormatter= [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:formatter];
return [dateFormatter dateFromString:dateString];
}
//时间戳转换成时间
NSInteger time = [[dic objectForKey:@"press_date"] intValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
NSString *timestr = [NSString stringWithFormat:@"%@",date];
NSRange rang;
rang.location = 0;
rang.length = 10;
NSString *needtime = [timestr substringWithRange:rang];