[英]How to create and store a new image file in cocoa
我想在磁盤上創建並存儲一個空白的白色圖像文件。 由於我是可可的新手,所以我不知道該怎么做。
我知道,您的問題與Mac有關,因為您添加了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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.