You need to log in to create posts and topics.
NSImageView
1,678 Posts
#1 · January 23, 2012, 4:52 pm
Quote from Forum Archives on January 23, 2012, 4:52 pmPosted by: artlythere <artlythere@...>
Looks like a (super) grand demo but I get an error? (I added Compile as "Objective-C" line, no change?)_1_NS_Image_View,NS_Image_View.h:14: error: expected identifier or '(' before '{' token_1_NS_Image_View,NS_Image_View.h:20: error: expected identifier or '(' before '{' tokenI looked online, the example had#import "NSImage-Utilities.h"#import "NSBezierPath-Utilities.h"?On Jan 23, 2012, at 12:34 PM, SVanVoorst wrote:
/*Source "Cocoa and Objective-C Up and Running", Scott Stevenson, ©2010, pp. 329-48.Converted to FBtoC by S.Van Voorst {20120122}*/include resources "MyImage.jpg"BeginCDeclaration@interface NSBezierPath (Utilities){ // Error right here}+ (NSBezierPath *) sheenPathForRect:(NSRect) myRect;@end //interfaceEndCBeginCFunction@implementation NSBezierPath (Utilities)+ (NSBezierPath *) sheenPathForRect:(NSRect) myRect{CGFloat minX = NSMinX(myRect);CGFloat maxX = NSMaxX(myRect);CGFloat maxY = NSMaxY(myRect);// Scale base of sheen with the image.CGFloat bottomLeftY = myRect.origin.y + ( myRect.size.height * 0.25 );CGFloat bottomRightY = myRect.origin.y + ( myRect.size.height * 0.9 );NSPoint point1 = NSMakePoint ( minX, bottomLeftY );NSPoint point2 = NSMakePoint ( maxX, bottomRightY );NSPoint point3 = NSMakePoint ( maxX, maxY );NSPoint point4 = NSMakePoint ( minX, maxY );// For arc that crosses the image.NSPoint control1 = NSMakePoint ( minX * 0.9, maxY * 0.9 );NSPoint control2 = NSMakePoint ( maxX * 0.9, maxY * 0.9 );// Create path.NSBezierPath *sheenPath = [NSBezierPath bezierPath];// Starting point.[sheenPath moveToPoint: point1];// Arc to other side.[sheenPath curveToPoint: point2 controlPoint1: control1 controlPoint2: control2];[sheenPath lineToPoint: point3];[sheenPath lineToPoint: point4];[sheenPath closePath];return sheenPath;}@end //implementationEndCBeginCDeclaration@interface NSImage (Utilities){}- (NSRect) proportionalRectForTargetRect:(NSRect) targetRect;@end //interfaceEndCBeginCFunction@implementation NSImage (Utilities)- (NSRect) proportionalRectForTargetRect:(NSRect) targetRect{// If the sizes are the same, we're already done //if (NSEqualSizes(self.size, targetRect.size)) return targetRect;NSSize imageSize = self.size;CGFloat sourceWidth = imageSize.width;CGFloat sourceHeight = imageSize.height;// Figure out difference in size for each side, and use the// larger adjustment for both edges to maintain aspect ratio.CGFloat widthAdjust = targetRect.size.width / sourceWidth;CGFloat heightAdjust = targetRect.size.height / sourceHeight;CGFloat scaleFactor = 1.0;if ( widthAdjust < heightAdjust ) scaleFactor = widthAdjust;else scaleFactor = widthAdjust;// Resize both edges by same amountCGFloat finalWidth = sourceWidth * scaleFactor;CGFloat finalHeight = sourceHeight * scaleFactor;NSSize finalSize = NSMakeSize( finalWidth, finalHeight );// Actual rect we'll use for the imageNSRect finalRect;finalRect.size = finalSize;// Use same base origin as target rect, but adjust for resized image.finalRect.origin = targetRect.origin;finalRect.origin.x += (targetRect.size.width - finalWidth) * 0.5;finalRect.origin.y += (targetRect.size.height - finalHeight) * 0.5;// Return exact coordinates for a sharp imagereturn NSIntegralRect (finalRect);}@end //implementationEndCBeginCDeclaration@interface AppDelegate : NSObject{NSImage *mainImage;NSColor *backgroundColor;NSColor *borderColor;NSWindow *window;}@property (retain) NSWindow *window;@property (retain) NSColor *backgroundColor;@property (retain) NSColor *borderColor;@property (retain) NSImage *mainImage;//methods automatically called@end //interfaceEndCBeginCFunction@implementation AppDelegate@synthesize backgroundColor;@synthesize borderColor;@synthesize mainImage;@synthesize window;-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *) theApp;{return YES;}- (void) applicationDidFinishLaunching:(NSNotification *) aNotification{NSRect viewFrame = [self.window.contentView bounds];StyledImageView *imageView;imageView = [[StyledImageView alloc] initWithFrame:viewFrame ];// Create background gradientNSColor *gradientBottom = [NSColor colorWithCalibratedWhite: 0.18 alpha: 1.0];NSColor *gradientTop = [NSColor colorWithCalibratedWhite: 0.35 alpha: 1.0];NSGradient *gradient = [[NSGradient alloc] initWithStartingColor: gradientBottom endingColor: gradientTop];[imageView setMainImage:mainImage];[imageView setBorderColor:borderColor];[imageView setBackgroundColor:backgroundColor];// ---- Set properties for this instance of the view ----- //imageView.mainImage = [NSImage imageNamed:@"MyImage.jpg"];imageView.backgroundColor = [NSColor darkGrayColor];imageView.borderColor = [NSColor whiteColor];imageView.shouldAddShadow = YES;imageView.backgroundGradient = gradient;imageView.shouldAddSheen = YES;[gradient release];// ---- Resize with the window ------ //NSInteger resizingMask = ( NSViewWidthSizable | NSViewHeightSizable );[imageView setAutoresizingMask: resizingMask ];// ---- Add to contentView and release ------ //[self.window.contentView addSubview:imageView];[imageView release];}@end //implementationEndCBeginCDeclaration@interface StyledImageView : NSView{NSImage *mainImage;NSColor *backgroundColor;NSColor *borderColor;NSShadow *imageShadow;BOOL shouldAddShadow;NSGradient *backgroundGradient;BOOL shouldAddSheen;NSGradient *imageSheen;}@property (retain) NSImage *mainImage;@property (retain) NSColor *backgroundColor;@property (retain) NSColor *borderColor;@property (retain) NSGradient *backgroundGradient;@property (assign) BOOL shouldAddShadow;@property (assign) BOOL shouldAddSheen;// private properties@property (retain) NSShadow *imageShadow;@property (retain) NSGradient *imageSheen;//private methods+ (NSShadow *) defaultImageShadow;+ (NSGradient *) defaultImageSheen;- (void) drawBackgroundInRect: (NSRect)rect;- (void) drawImageSheenInRect: (NSRect)rect;@end //interfaceEndCBeginCFunction@implementation StyledImageView : NSView;@synthesize mainImage;@synthesize backgroundColor;@synthesize borderColor;@synthesize imageShadow;@synthesize backgroundGradient;@synthesize imageSheen;// Use @dynamic since we created the setters@dynamic shouldAddShadow;@dynamic shouldAddSheen;- (id)initWithFrame:(NSRect)frameRect{if ((self = [super initWithFrame:frameRect]) != nil){self = [super initWithFrame:frameRect];// Add initialization code here}return self;}- (void) drawBackgroundInRect: (NSRect)rect{// Draw background[self.backgroundColor set];NSRectFill ( rect );// Draw background gradient[self.backgroundGradient drawInRect:rect angle:90.0];}- (void) drawImageSheenInRect: (NSRect)rect{NSBezierPath *sheenPath = [NSBezierPath sheenPathForRect:rect];// Draw at 280.0 degrees to simulate light source from upper-left.[self.imageSheen drawInBezierPath: sheenPath angle:280.0 ];}- (void)drawRect:(NSRect)rect{NSRect bounds = self.bounds;[self drawBackgroundInRect:bounds];CGFloat insetX = NSWidth ( bounds ) * 0.10;CGFloat insetY = NSHeight ( bounds ) * 0.10;NSRect imageRect = NSInsetRect ( bounds, insetX, insetY );// ***** Draw the Image ****** //NSImage *image = self.mainImage;imageRect = [image proportionalRectForTargetRect:imageRect];// Save graphics context, apply the shadow, and draw image.[NSGraphicsContext saveGraphicsState];[self.imageShadow set];[ image drawInRect: imageRectfromRect: NSZeroRectoperation: NSCompositeSourceOverfraction: 1.0 ];[NSGraphicsContext restoreGraphicsState];// Draw the sheen[self drawImageSheenInRect:imageRect];// Draw border around image.[self.borderColor set];NSBezierPath *imageFrame = [NSBezierPath bezierPathWithRect: imageRect ];imageFrame.lineWidth = 4.0;[imageFrame stroke];}- (void) dealloc{self.mainImage = nil;self.backgroundColor = nil;self.borderColor = nil;self.imageShadow = nil;self.backgroundGradient = nil;self.imageSheen = nil;[super dealloc];}// Use this if you want 0,0 (origin) to be top, left// Otherwise origin will be at bottom, left (Unflipped)-(BOOL)isFlipped{return NO;}- (void) setShouldAddShadow:(BOOL)shouldAdd{// If new value is same, just return.if (shouldAddShadow == shouldAdd) return;// Set new valueshouldAddShadow = shouldAdd;if (shouldAddShadow) self.imageShadow = [[self class] defaultImageShadow];else self.imageShadow = nil;// Redraw[self setNeedsDisplay:YES];}- (void) setShouldAddSheen:(BOOL)shouldAdd{// If new value is same, just return.if (shouldAddSheen == shouldAdd) return;// Set new valueshouldAddSheen = shouldAdd;if (shouldAddSheen) self.imageSheen = [[self class] defaultImageSheen];else self.imageSheen = nil;// Redraw[self setNeedsDisplay:YES];}#pragma mark Private+ (NSShadow *) defaultImageShadow{NSShadow *newShadow = [[NSShadow alloc] init];newShadow.shadowBlurRadius = 8.0;newShadow.shadowOffset = NSMakeSize( 0,-6 );newShadow.shadowColor = [NSColor blackColor];return [newShadow autorelease];}+ (NSGradient *) defaultImageSheen{NSColor *color1 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.80];NSColor *color2 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.10];NSColor *color3 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.0];// 0.0 = start of gradient; 1.0 = endNSGradient *sheen = [[NSGradient alloc] initWithColorsAndLocations:color1, 0.0, color2, 0.4, color3, 0.8, nil ];return [sheen autorelease];}@end //implementationEndCBeginCCode{NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[NSApplication sharedApplication];[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];// ******* Menu and menubar ******* //id menubar = [[NSMenu new] autorelease];id appMenuItem = [[NSMenuItem new] autorelease];[menubar addItem:appMenuItem];[NSApp setMainMenu:menubar];id appMenu = [[NSMenu new] autorelease];id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"] autorelease];[appMenu addItem:quitMenuItem];[appMenuItem setSubmenu:appMenu];// ****** Window ****** //#define _wndW 500#define _wndH 400id window = [ [ NSWindow alloc ]initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )styleMask: NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMaskbacking:NSBackingStoreBuffereddefer:NO];[window center];[window setTitle: @"NSImage" ];[window makeKeyAndOrderFront:nil];// -- Create instance of AppDelegate (NSObject) ----- //id myDelegate = [[AppDelegate alloc]init];[NSApp setDelegate:myDelegate];[myDelegate setWindow: window];// -- Instance of StyledImageView (NSView) Created by AppDelegate ---- //[NSApp activateIgnoringOtherApps:YES];[NSApp run];[pool drain];}EndC
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Posted by: artlythere <artlythere@...>
Looks like a (super) grand demo but I get an error? (I added Compile as "Objective-C" line, no change?)
_1_NS_Image_View,NS_Image_View.h:14: error: expected identifier or '(' before '{' token
_1_NS_Image_View,NS_Image_View.h:20: error: expected identifier or '(' before '{' token
I looked online, the example had
#import "NSImage-Utilities.h"
#import "NSBezierPath-Utilities.h"
?
On Jan 23, 2012, at 12:34 PM, SVanVoorst wrote:
/*Source "Cocoa and Objective-C Up and Running", Scott Stevenson, ©2010, pp. 329-48.Converted to FBtoC by S.Van Voorst {20120122}*/include resources "MyImage.jpg"BeginCDeclaration@interface NSBezierPath (Utilities){ // Error right here}+ (NSBezierPath *) sheenPathForRect:(NSRect) myRect;@end //interfaceEndCBeginCFunction@implementation NSBezierPath (Utilities)+ (NSBezierPath *) sheenPathForRect:(NSRect) myRect{CGFloat minX = NSMinX(myRect);CGFloat maxX = NSMaxX(myRect);CGFloat maxY = NSMaxY(myRect);// Scale base of sheen with the image.CGFloat bottomLeftY = myRect.origin.y + ( myRect.size.height * 0.25 );CGFloat bottomRightY = myRect.origin.y + ( myRect.size.height * 0.9 );NSPoint point1 = NSMakePoint ( minX, bottomLeftY );NSPoint point2 = NSMakePoint ( maxX, bottomRightY );NSPoint point3 = NSMakePoint ( maxX, maxY );NSPoint point4 = NSMakePoint ( minX, maxY );// For arc that crosses the image.NSPoint control1 = NSMakePoint ( minX * 0.9, maxY * 0.9 );NSPoint control2 = NSMakePoint ( maxX * 0.9, maxY * 0.9 );// Create path.NSBezierPath *sheenPath = [NSBezierPath bezierPath];// Starting point.[sheenPath moveToPoint: point1];// Arc to other side.[sheenPath curveToPoint: point2 controlPoint1: control1 controlPoint2: control2];[sheenPath lineToPoint: point3];[sheenPath lineToPoint: point4];[sheenPath closePath];return sheenPath;}@end //implementationEndCBeginCDeclaration@interface NSImage (Utilities){}- (NSRect) proportionalRectForTargetRect:(NSRect) targetRect;@end //interfaceEndCBeginCFunction@implementation NSImage (Utilities)- (NSRect) proportionalRectForTargetRect:(NSRect) targetRect{// If the sizes are the same, we're already done //if (NSEqualSizes(self.size, targetRect.size)) return targetRect;NSSize imageSize = self.size;CGFloat sourceWidth = imageSize.width;CGFloat sourceHeight = imageSize.height;// Figure out difference in size for each side, and use the// larger adjustment for both edges to maintain aspect ratio.CGFloat widthAdjust = targetRect.size.width / sourceWidth;CGFloat heightAdjust = targetRect.size.height / sourceHeight;CGFloat scaleFactor = 1.0;if ( widthAdjust < heightAdjust ) scaleFactor = widthAdjust;else scaleFactor = widthAdjust;// Resize both edges by same amountCGFloat finalWidth = sourceWidth * scaleFactor;CGFloat finalHeight = sourceHeight * scaleFactor;NSSize finalSize = NSMakeSize( finalWidth, finalHeight );// Actual rect we'll use for the imageNSRect finalRect;finalRect.size = finalSize;// Use same base origin as target rect, but adjust for resized image.finalRect.origin = targetRect.origin;finalRect.origin.x += (targetRect.size.width - finalWidth) * 0.5;finalRect.origin.y += (targetRect.size.height - finalHeight) * 0.5;// Return exact coordinates for a sharp imagereturn NSIntegralRect (finalRect);}@end //implementationEndCBeginCDeclaration@interface AppDelegate : NSObject{NSImage *mainImage;NSColor *backgroundColor;NSColor *borderColor;NSWindow *window;}@property (retain) NSWindow *window;@property (retain) NSColor *backgroundColor;@property (retain) NSColor *borderColor;@property (retain) NSImage *mainImage;//methods automatically called@end //interfaceEndCBeginCFunction@implementation AppDelegate@synthesize backgroundColor;@synthesize borderColor;@synthesize mainImage;@synthesize window;-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *) theApp;{return YES;}- (void) applicationDidFinishLaunching:(NSNotification *) aNotification{NSRect viewFrame = [self.window.contentView bounds];StyledImageView *imageView;imageView = [[StyledImageView alloc] initWithFrame:viewFrame ];// Create background gradientNSColor *gradientBottom = [NSColor colorWithCalibratedWhite: 0.18 alpha: 1.0];NSColor *gradientTop = [NSColor colorWithCalibratedWhite: 0.35 alpha: 1.0];NSGradient *gradient = [[NSGradient alloc] initWithStartingColor: gradientBottom endingColor: gradientTop];[imageView setMainImage:mainImage];[imageView setBorderColor:borderColor];[imageView setBackgroundColor:backgroundColor];// ---- Set properties for this instance of the view ----- //imageView.mainImage = [NSImage imageNamed:@"MyImage.jpg"];imageView.backgroundColor = [NSColor darkGrayColor];imageView.borderColor = [NSColor whiteColor];imageView.shouldAddShadow = YES;imageView.backgroundGradient = gradient;imageView.shouldAddSheen = YES;[gradient release];// ---- Resize with the window ------ //NSInteger resizingMask = ( NSViewWidthSizable | NSViewHeightSizable );[imageView setAutoresizingMask: resizingMask ];// ---- Add to contentView and release ------ //[self.window.contentView addSubview:imageView];[imageView release];}@end //implementationEndCBeginCDeclaration@interface StyledImageView : NSView{NSImage *mainImage;NSColor *backgroundColor;NSColor *borderColor;NSShadow *imageShadow;BOOL shouldAddShadow;NSGradient *backgroundGradient;BOOL shouldAddSheen;NSGradient *imageSheen;}@property (retain) NSImage *mainImage;@property (retain) NSColor *backgroundColor;@property (retain) NSColor *borderColor;@property (retain) NSGradient *backgroundGradient;@property (assign) BOOL shouldAddShadow;@property (assign) BOOL shouldAddSheen;// private properties@property (retain) NSShadow *imageShadow;@property (retain) NSGradient *imageSheen;//private methods+ (NSShadow *) defaultImageShadow;+ (NSGradient *) defaultImageSheen;- (void) drawBackgroundInRect: (NSRect)rect;- (void) drawImageSheenInRect: (NSRect)rect;@end //interfaceEndCBeginCFunction@implementation StyledImageView : NSView;@synthesize mainImage;@synthesize backgroundColor;@synthesize borderColor;@synthesize imageShadow;@synthesize backgroundGradient;@synthesize imageSheen;// Use @dynamic since we created the setters@dynamic shouldAddShadow;@dynamic shouldAddSheen;- (id)initWithFrame:(NSRect)frameRect{if ((self = [super initWithFrame:frameRect]) != nil){self = [super initWithFrame:frameRect];// Add initialization code here}return self;}- (void) drawBackgroundInRect: (NSRect)rect{// Draw background[self.backgroundColor set];NSRectFill ( rect );// Draw background gradient[self.backgroundGradient drawInRect:rect angle:90.0];}- (void) drawImageSheenInRect: (NSRect)rect{NSBezierPath *sheenPath = [NSBezierPath sheenPathForRect:rect];// Draw at 280.0 degrees to simulate light source from upper-left.[self.imageSheen drawInBezierPath: sheenPath angle:280.0 ];}- (void)drawRect:(NSRect)rect{NSRect bounds = self.bounds;[self drawBackgroundInRect:bounds];CGFloat insetX = NSWidth ( bounds ) * 0.10;CGFloat insetY = NSHeight ( bounds ) * 0.10;NSRect imageRect = NSInsetRect ( bounds, insetX, insetY );// ***** Draw the Image ****** //NSImage *image = self.mainImage;imageRect = [image proportionalRectForTargetRect:imageRect];// Save graphics context, apply the shadow, and draw image.[NSGraphicsContext saveGraphicsState];[self.imageShadow set];[ image drawInRect: imageRectfromRect: NSZeroRectoperation: NSCompositeSourceOverfraction: 1.0 ];[NSGraphicsContext restoreGraphicsState];// Draw the sheen[self drawImageSheenInRect:imageRect];// Draw border around image.[self.borderColor set];NSBezierPath *imageFrame = [NSBezierPath bezierPathWithRect: imageRect ];imageFrame.lineWidth = 4.0;[imageFrame stroke];}- (void) dealloc{self.mainImage = nil;self.backgroundColor = nil;self.borderColor = nil;self.imageShadow = nil;self.backgroundGradient = nil;self.imageSheen = nil;[super dealloc];}// Use this if you want 0,0 (origin) to be top, left// Otherwise origin will be at bottom, left (Unflipped)-(BOOL)isFlipped{return NO;}- (void) setShouldAddShadow:(BOOL)shouldAdd{// If new value is same, just return.if (shouldAddShadow == shouldAdd) return;// Set new valueshouldAddShadow = shouldAdd;if (shouldAddShadow) self.imageShadow = [[self class] defaultImageShadow];else self.imageShadow = nil;// Redraw[self setNeedsDisplay:YES];}- (void) setShouldAddSheen:(BOOL)shouldAdd{// If new value is same, just return.if (shouldAddSheen == shouldAdd) return;// Set new valueshouldAddSheen = shouldAdd;if (shouldAddSheen) self.imageSheen = [[self class] defaultImageSheen];else self.imageSheen = nil;// Redraw[self setNeedsDisplay:YES];}#pragma mark Private+ (NSShadow *) defaultImageShadow{NSShadow *newShadow = [[NSShadow alloc] init];newShadow.shadowBlurRadius = 8.0;newShadow.shadowOffset = NSMakeSize( 0,-6 );newShadow.shadowColor = [NSColor blackColor];return [newShadow autorelease];}+ (NSGradient *) defaultImageSheen{NSColor *color1 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.80];NSColor *color2 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.10];NSColor *color3 = [NSColor colorWithDeviceWhite: 1.0 alpha: 0.0];// 0.0 = start of gradient; 1.0 = endNSGradient *sheen = [[NSGradient alloc] initWithColorsAndLocations:color1, 0.0, color2, 0.4, color3, 0.8, nil ];return [sheen autorelease];}@end //implementationEndCBeginCCode{NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[NSApplication sharedApplication];[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];// ******* Menu and menubar ******* //id menubar = [[NSMenu new] autorelease];id appMenuItem = [[NSMenuItem new] autorelease];[menubar addItem:appMenuItem];[NSApp setMainMenu:menubar];id appMenu = [[NSMenu new] autorelease];id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"] autorelease];[appMenu addItem:quitMenuItem];[appMenuItem setSubmenu:appMenu];// ****** Window ****** //#define _wndW 500#define _wndH 400id window = [ [ NSWindow alloc ]initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )styleMask: NSClosableWindowMask | NSTitledWindowMask | NSResizableWindowMaskbacking:NSBackingStoreBuffereddefer:NO];[window center];[window setTitle: @"NSImage" ];[window makeKeyAndOrderFront:nil];// -- Create instance of AppDelegate (NSObject) ----- //id myDelegate = [[AppDelegate alloc]init];[NSApp setDelegate:myDelegate];[myDelegate setWindow: window];// -- Instance of StyledImageView (NSView) Created by AppDelegate ---- //[NSApp activateIgnoringOtherApps:YES];[NSApp run];[pool drain];}EndC
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
Click for thumbs down.0Click for thumbs up.0