Cocoa的几种内置数据类型之间的互转

因为Cocoa的数据类型之间的互转在实际程序中应用很广泛,因此在此记录一下常用的几个互转。

NSDataNSString之间互转:

1
2
3
4
5
6
7
8
9
/* NSString to NSData */
NSString *theString = @"This is a string";
NSData *theStringData = [theString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@\n%@", theString, theStringData);

/* NSData to NSString */
NSString *theStringFromData = [[NSString alloc] initWithData:theStringData encoding:NSUTF8StringEncoding];
NSLog(@"%@", theStringFromData);
[theStringFromData release];

这里用NSUTF8StringEncoding作为例子,实际可以使用更多其它的编码类型,详情可以参考文档。

2 从某个路径(NSString)加载图片

1
2
3
4
NSString *imagePath = [[NSBundle mainBundle] pathForImageResource:@"some_image"];
NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
//[self.imageWell setImage:theImage];
[theImage release];

除了可以从NSString加载图片,也可以从NSURL加载。方法类似。

NSDataNSImage之间互转:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* NSImage to NSData (TIFF) */
NSData *someData = [NSData dataWithContentsOfFile:imagePath];
NSImage *someImage = [[NSImage alloc] initWithData:someData];
NSData *dataFromImage = [someImage TIFFRepresentation];
NSMutableString *homeDir = [[NSMutableString alloc] initWithString:NSHomeDirectory()];
[dataFromImage writeToFile:[homeDir stringByAppendingPathComponent:@"Desktop/image.tiff"] atomically:NO];

/* NSImage to NSData (Any Format, use png as example) */
//NSBitmapImageRep *bmprep = [NSBitmapImageRep imageRepWithData:dataFromImage]; // TIFF representation data
NSBitmapImageRep *bmprep = [NSBitmapImageRep imageRepWithData:someData]; // PNG data from file
NSDictionary *imageProperty = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.0], NSImageCompressionFactor, nil];
NSData *otherTypeImageData = [bmprep representationUsingType:NSPNGFileType properties:imageProperty];
[otherTypeImageData writeToFile:[homeDir stringByAppendingPathComponent:@"Desktop/image.png"] atomically:NO];
[homeDir release];
[someImage release];

NSDataNSImage之间互转实际上相当复杂。这里使用了NSBitmapImageRep作为中间对象来完成对各种图片类型的支持(主要包括TIFF,JPEG,GIF和PNG)。特别是imageProperty这个NSDictionary,在实际使用中需要参考文档进行赋值。

NSURL和`NSString之间互转:

1
2
3
4
5
6
7
8
/* NSString to NSURL */
NSString *google = @"http://google.com";
NSURL *googleURL = [[NSURL alloc] initWithString:google];
NSLog(@"%@", googleURL);

/* NSURL to NSString */
NSLog(@"%@, %@", [googleURL relativeString], [googleURL absoluteString]);
[googleURL release];

NSStringNSURL没什么说的;从NSURLNSString,有两个方法,一个是绝对路径,一个是相对路径。对于本地文件的路径,这两个方法是有区别的;而对于网页等远程路径,这两个方法返回的值相同。

CGImageNSImage

1
2
3
4
5
6
7
/* CGImage to NSImage */
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
[imageView setImage:image];
[image release];

      
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值