Modifying an Image and representing it in a Screen Saver
Modifying an Image and representing it in a Screen Saver
- Subject: Modifying an Image and representing it in a Screen Saver
- From: "James Kent" <email@hidden>
- Date: Fri, 16 Sep 2005 10:02:19 +1000
Hi,
I am currently learning Obj-C and Cocoa and have been working through
some Screen Saver example code I found on the web.
Basically, I am trying to build an image in memory and then represent
it on the Screen Saver's view (if that is indeed the correct
terminology). At present, the code should draw an external image,
whose dimensions have been altered, on the buffered image and a
random shape. However, it is showing the random shape no image but
won't have a bar of the external image. Also worth noting, I am
modifying the background colour of the buffered image.
Below is the code of the animateOneFrame method:
- (void)animateOneFrame;
{
//
------------------------------------------------------------------------
------------
NSRect rect;
rect.size = NSMakeSize( SSRandomFloatBetween( [self
bounds].size.width/100, [self bounds].size.width/10 ),
SSRandomFloatBetween( [self
bounds].size.height/100, [self bounds].size.height/10 ));
rect.origin = SSRandomPointForSizeWithinRect( rect.size, [self
bounds] );
NSBezierPath *path;
if (SSRandomIntBetween( 0, 1 ) == 0) {
path = [NSBezierPath bezierPathWithRect:rect];
} else {
path = [NSBezierPath bezierPathWithOvalInRect:rect];
}
NSColor *color = [NSColor colorWithCalibratedRed:
(SSRandomFloatBetween( 0.0, 255.0 ) / 255.0)
green:(SSRandomFloatBetween( 0.0, 255.0 ) /
255.0)
blue:(SSRandomFloatBetween( 0.0, 255.0 ) /
255.0)
alpha:(SSRandomFloatBetween( 0.0, 255.0 ) /
255.0)];
//
------------------------------------------------------------------------
------------
NSImage *image;
NSString *fileName;
if (SSRandomIntBetween( 0, 1 ) == 0) {
fileName = @"/Users/jkentjnr/Desktop/test.jpg";
} else {
fileName = @"/Users/jkentjnr/Desktop/mel1.jpg";
}
image = [[NSImage alloc] initWithContentsOfFile:fileName];
//
------------------------------------------------------------------------
------------
NSImage *imgBuffer;
imgBuffer = [[NSImage alloc] initWithSize:[self bounds].size];
[imgBuffer lockFocus]; // Draw on the new Image.
// Set the Background Colour
[imgBuffer setBackgroundColor:[NSColor colorWithCalibratedRed:
0.0 green:0.0 blue:0.5 alpha:0.5]];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext]
setImageInterpolation:NSImageInterpolationHigh];
[imgBuffer drawInRect:NSMakeRect(0.0,0.0,200,200)
fromRect:NSMakeRect(0.0,0.0,400,400) operation:NSCompositeCopy
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[color set];
// And finally draw it
[path fill];
[imgBuffer unlockFocus];
[self lockFocus];
[imgBuffer drawInRect:[self bounds] fromRect:NSMakeRect(0.0,0.0,
[imgBuffer size].width,[imgBuffer size].height)
operation:NSCompositeCopy fraction:0.3];
//
------------------------------------------------------------------------
------------
[imgBuffer release];
[image release];
}
Many thanks for helping a passionate Newbie,
James
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden