Objective c 如何在cocoa中创建和存储新图像文件
我想在磁盘上创建并存储一个空的白色图像文件。因为我是cocoa新手,我不知道怎么做。我明白了,你的问题与Mac有关,因为你添加了Objective c 如何在cocoa中创建和存储新图像文件,objective-c,cocoa,nsimage,Objective C,Cocoa,Nsimage,我想在磁盘上创建并存储一个空的白色图像文件。因为我是cocoa新手,我不知道怎么做。我明白了,你的问题与Mac有关,因为你添加了cocoa和NSImage标签。 所以试试这个 NSRect kRect = NSMakeRect(0,0,100,100); NSView *view = [[NSView alloc] initWithFrame:kRect]; CALayer *viewLayer = [CALayer layer]; [viewLayer set
cocoa
和NSImage
标签。
所以试试这个
NSRect kRect = NSMakeRect(0,0,100,100);
NSView *view = [[NSView alloc] initWithFrame:kRect];
CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(1, 0.0, 0.0, 1)]; //RGB plus Alpha Channel
[view setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
[view setLayer:viewLayer];
// [self.window.contentView addSubview:view];
NSBitmapImageRep* kRep = [view bitmapImageRepForCachingDisplayInRect:kRect];
[view cacheDisplayInRect:kRect toBitmapImageRep:kRep];
NSData* kData = [kRep representationUsingType:NSPNGFileType properties:nil];
[kData writeToFile:@"/Users/MyName/Desktop/red.png" atomically:YES];