Objaśnienie / konwersja systemu iOS na Mac GraphicContext

Programuję od dwóch lat na iOS i nigdy na mac. Pracuję nad małym narzędziem do obsługi niektórych prostych potrzeb związanych z obrazem, które posiadam w moim rozwoju iOS. W każdym razie mam działający kod w iOS, który działa doskonale, ale nie mam absolutnie żadnego pojęcia, jakie są odpowiedniki dla mac.

Próbowałem kilku różnych rzeczy, ale naprawdę nie rozumiem, jak uruchomić kontekst graficzny na Macu poza metodą „drawRect:”. W iPhonie używałbym po prostu UIGraphicsBeghinImageContext (). Wiem, że inny post powiedział, aby użyć lockFocus / unlockFocus, ale nie jestem pewien, jak dokładnie wykonać tę pracę dla moich potrzeb. Aha, naprawdę tęsknię za własnością „CGImage” UIImage. Nie rozumiem, dlaczego NSImage nie może go mieć, choć brzmi to trochę trudniej niż tylko to.

Oto mój działający kod na iOS - po prostu tworzy obraz odbity z maski i łączy je razem:

    UIImage *mask = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Mask_Image.jpg" ofType:nil]];
    UIImage *image = [UIImage imageNamed::@"Test_Image1.jpg"];
    UIGraphicsBeginImageContextWithOptions(mask.size, NO, [[UIScreen mainScreen]scale]);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0.0, mask.size.height);
    CGContextScaleCTM(ctx, 1.f, -1.f);
    [image drawInRect:CGRectMake(0.f, -mask.size.height, image.size.width, image.size.height)];
    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef maskRef = mask.CGImage;
    CGImageRef maskCreate = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([flippedImage CGImage], maskCreate);

    CGImageRelease(maskCreate);

    UIImage *maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height + (image.size.height * .5)), NO, [[UIScreen mainScreen]scale]);

    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
    [maskedImage drawInRect:CGRectMake(0, image.size.height, maskedImage.size.width, maskedImage.size.height)];

    UIImage *anotherImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); 

//do something with anotherImage

Wszelkie sugestie dotyczące osiągnięcia tego (po prostu) na komputerze Mac?

questionAnswers(4)

yourAnswerToTheQuestion