Ответы пользователя по тегу Swift
  • Как создать изображение в Xcode + Swift?

    @denisk0n
    Creative technology
    "Пища для ума" (с)
    Синий прямоугольник, но только Xcode + Objective-C
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char *argv[]) {
        const NSUInteger WIDTH = 100;
        const NSUInteger HEIGHT = 200;
        const NSUInteger COMPONENTS_PER_PIXEL = 4;
        const NSUInteger BITS_PER_COMPONENT = 8;
        const NSUInteger BUFFER_LENGTH = WIDTH * HEIGHT * COMPONENTS_PER_PIXEL;
    
        char *buffer = (char *) malloc(BUFFER_LENGTH);
        for (NSUInteger i = 0; i < BUFFER_LENGTH; i += 4) {
            buffer[i + 0] = 0x00;
            buffer[i + 1] = 0x00;
            buffer[i + 2] = 0xFF;
            buffer[i + 3] = 0xFF;
        }
    
        CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, buffer, BUFFER_LENGTH, NULL);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGImageRef image = CGImageCreate(WIDTH, HEIGHT, BITS_PER_COMPONENT, BITS_PER_COMPONENT * COMPONENTS_PER_PIXEL, COMPONENTS_PER_PIXEL * WIDTH, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaLast, dataProvider, NULL, false, kCGRenderingIntentDefault);
    
        NSString *filePath = @"~/test.png";
        NSURL *url = [NSURL fileURLWithPath:[filePath stringByExpandingTildeInPath]];
        CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef) url, kUTTypePNG, 1, NULL);
    
        CGImageDestinationAddImage(imageDestination, image, NULL);
    
        CGImageDestinationFinalize(imageDestination);
        CGImageRelease(image);
        CGColorSpaceRelease(colorSpace);
        CGDataProviderRelease(dataProvider);
        free(buffer);
    
        return 0;
    }
    Ответ написан
    4 комментария