【问题标题】:How to create and store a new image file in cocoa如何在可可中创建和存储新的图像文件
【发布时间】:2014-07-17 20:30:46
【问题描述】:

我想在磁盘上创建并存储一个空白图像文件。由于我是可可新手,我不知道该怎么做。

【问题讨论】:

    标签: objective-c cocoa nsimage


    【解决方案1】:

    我明白了,您的问题与 Mac 有关,因为您添加了 cocoaNSImage 标签。 所以试试这个。

        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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 2011-09-28
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      相关资源
      最近更新 更多