الكاكاو: باستخدام nsgradient كقناع للرسم أدناه
-
29-09-2019 - |
سؤال
أحاول رسم انعكاس الشكل باستخدام الكاكاو. لقد قمت بالفعل بتطبيق nsaffinetransform وأعيد رسم الشكل بنجاح ، لكن الآن لا يمكنني معرفة كيفية رسم قناع ألفا فوقه. أنا أستخدم NSCompositeDestinationOut
العملية ، لكنها تعطيني نتيجة غير مرغوب فيها:alt text http://img687.imageshack.us/img687/2417/capturedcran20100623094.png
لست متأكدًا تمامًا من كيفية إصلاح هذا - أحتاج إلى صنعه لذا فإن التدرج يعمل فقط كقناع ألفا ولا يتم عرضه بالفعل. هل أستخدم وضع التركيب الخاطئ؟
شكرًا! إليك رمز التدرج إذا لزم الأمر:
- (void)fadeOutRect:(NSRect)rect {
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeDestinationOut];
NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:
[[NSColor blackColor] colorWithAlphaComponent:0.5], 0.0,
[[NSColor blackColor] colorWithAlphaComponent:1.0], 0.8, nil];
[gradient drawInRect:NSMakeRect(rect.origin.x, rect.origin.y + rect.size.height - ( PILL_HEIGHT * 2 ),
rect.size.width, PILL_HEIGHT) angle:270];
[NSGraphicsContext restoreGraphicsState];
}
المحلول
نعم. إليك مثال على الكود الذي نستخدمه للقيام بذلك. يستخدم صورة مصدر ، ولديه القليل من الأعمال المضحكة بعامل المقياس ، ولكن يجب أن تكون قادرًا على استخدام الهيكل الأساسي وخيارات التركيب للقيام بما تحتاجه. (هذا الرمز موجود ضمن كتلة [انعكاس lockfocus] ، والنفس هو nsimage الذي نقوم به انعكاس.)
// Draw our mask into the image
NSGradient* fade = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]
endingColor:[NSColor clearColor]];
[fade drawFromPoint:NSMakePoint(0.0, size.height)
toPoint:NSMakePoint(0.0, 0.0)
options:0];
// Composite the original image, upside-down
NSAffineTransform* flipper = [NSAffineTransform transform];
[flipper scaleXBy:1.0 yBy:-1.0];
[flipper concat];
[self drawInRect:NSMakeRect(0.0, -1.0*size.height, size.width, size.height)
fromRect:NSMakeRect(0.0, 0.0, self.size.width, size.height / scaleFactor)
operation:NSCompositeSourceIn fraction:1.0];