- static inline double rad (int alpha)
- {
- return ((alpha * pi)/180);
- }
- static CGImageRef RotateImageWithFlip (CGImageRef source, float angle,
- BOOL flipX, BOOL flipY)
- {
- float fX = fabs ( cos ( rad ( angle ) ) );
- float fY = fabs ( sin ( rad ( angle ) ) );
- float dW = (float)CGImageGetWidth (source) * fX +
- (float)CGImageGetHeight (source) * fY;
- float dH = (float)CGImageGetWidth (source) * fY +
- (float)CGImageGetHeight (source) * fX;
- CGContextRef context = CGBitmapContextCreate (NULL,
- (size_t)dW, (size_t)dH,
- CGImageGetBitsPerComponent(source),
- 0,
- CGImageGetColorSpace(source),
- kCGImageAlphaPremultipliedLast);
- CGContextSetAllowsAntialiasing(context, NO);
- CGContextSetShouldAntialias(context, NO);
- CGContextSetInterpolationQuality (context, kCGInterpolationLow);
- CGAffineTransform transform =
- CGAffineTransformMakeTranslation(flipX?dW:0.0f,flipY?dH:0.0f);
- transform = CGAffineTransformScale (transform,flipX?-1.0:1.0f,flipY?-1.0: 1.0f);
- if (0.0f != angle)
- {
- CGAffineTransform rot = CGAffineTransformMakeTranslation (dW*0.5f,dH*0.5f);
- rot = CGAffineTransformRotate(rot, rad ( angle ));
- rot = CGAffineTransformTranslate (rot,-dH*0.5f,-dW*0.5f);
- transform = CGAffineTransformConcat(rot, transform);
- }
- CGContextConcatCTM(context, transform);
- CGContextDrawImage(context, CGRectMake (0, 0, CGImageGetWidth (source),
- CGImageGetHeight (source)), source);
- CGContextFlush(context);
- CGImageRef rotated = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- return rotated;
- }
- static CGImageRef RotateWithEXIF (CGImageRef source, int orientation)
- {
- switch (orientation)
- {
- case 2:
- return RotateImageWithFlip (source, 0, YES, NO);
- break;
- case 3:
- return RotateImageWithFlip (source, 0, YES, YES);
- break;
- case 4:
- return RotateImageWithFlip (source, 0, NO, YES);
- break;
- case 5:
- return RotateImageWithFlip (source, 90, NO, YES);
- break;
- case 6:
- return RotateImageWithFlip (source, -90, NO, NO);
- break;
- case 7:
- return RotateImageWithFlip (source, 90, YES, NO);
- break;
- case 8:
- return RotateImageWithFlip (source, -90, YES, YES);
- break;
- default:
- break;
- }
- return NULL;
- }
Показаны сообщения с ярлыком Cocoa. Показать все сообщения
Показаны сообщения с ярлыком Cocoa. Показать все сообщения
вторник, 11 сентября 2012 г.
Create CGImageRef with EXIF Orientation
вторник, 7 августа 2012 г.
Preparing applications for 10.8 (HiDPI)
All you need to make the developers, it is properly configured to work with the regime HiDPI.
About the class NSImage.
All the work is not in pixels but in points. Released two QuartzCore used to work with all images or do a little fix-resize the output image.
Do not forget about the icon. This designer's work - painting the icons in the amount of 1024x1024px.
If the application is actively using OpenGL.
When initializing the NS*View is bound to 3D-context, do check the retina.
- #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
- if ( [ NSApplication usingLionOSX ] )
- {
- NSRect baseBounds = [ self bounds ];
- NSRect hdpiBounds = [ self convertRectToBacking : baseBounds ];
- if (NO == NSEqualSizes(baseBounds.size, hdpiBounds.size))
- {
- [ self setWantsBestResolutionOpenGLSurface : YES ];
- }
- }
- #endif
If you change the size of the application recalculate dimensions.
- - (void) updateBounds
- {
- NSRect baseBounds = [ self bounds ];
- #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
- if ( [ NSApplication usingLionOSX ] )
- {
- baseBounds = [ self convertRectToBacking : baseBounds ];
- }
- #endif
- ....
- glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f );
- glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glViewport (0.0f, 0.0f, baseBounds.size.width, baseBounds.size.height );
- ....
- }
About the class NSImage.
All the work is not in pixels but in points. Released two QuartzCore used to work with all images or do a little fix-resize the output image.
- NSImageRep* rep = [[img representations] objectAtIndex:0]; // fix HDPI
- if (rep)
- {
- NSSize size = NSMakeSize ([rep pixelsWide], [rep pixelsHigh]);
- [img setSize : size];
- }
Resources and rewards.
All UI elements are presented in the form of bitmap images must be properly scaled up twice. XCode automatically to pick up, just add '* texture.name * @ 2x.tiff ' copy of your texture to the project. If you need to manually create bitmaps from the resource that is the method of [NSimage imageNamed: @ "* texture.name *"] correctly load the desired image.
Do not forget about the icon. This designer's work - painting the icons in the amount of 1024x1024px.
пятница, 2 марта 2012 г.
How to load custom font from file (NSFont from file)
Load a NSFont from bundle.
First, add ApplicationServices.framework to project.
First, add ApplicationServices.framework to project.
- + (CTFontRef) fontFromBundle : (NSString*) fontName withHeight : (CGFloat) height;
- {
- // Get the path to our custom font and create a data provider.
- NSString* fontPath = [[NSBundle mainBundle] pathForResource : fontName
- ofType : @"ttf" ];
- if (nil==fontPath)
- return NULL;
- CGDataProviderRef dataProvider =
- CGDataProviderCreateWithFilename ([fontPath UTF8String]);
- if (NULL==dataProvider)
- return NULL;
- // Create the font with the data provider, then release the data provider.
- CGFontRef fontRef = CGFontCreateWithDataProvider ( dataProvider );
- if ( NULL == fontRef )
- {
- CGDataProviderRelease ( dataProvider );
- return NULL;
- }
- CTFontRef fontCore = CTFontCreateWithGraphicsFont(fontRef, height, NULL, NULL);
- CGDataProviderRelease (dataProvider);
- CGFontRelease(fontRef);
- return fontCore;
- }
In code
- CTFontRef bundleFont = [ CustomFonts fontFromBundle : @"MavenPro-Regular"
- withHeight : 25 ];
- NSFont* font = (NSFont*)bundleFont;
- ....
- // use NSFont
- ....
- CFRelease(bundleFont);
UPD. works correctly only 10.7+
понедельник, 5 сентября 2011 г.
Manually set the full-screen mode at startup application in Lion 10.7
- ................
- #import <CoreServices/CoreServices.h>
- @implementation NSApplication (SystemVersion)
- + (BOOL) usingLionOSX
- {
- SInt32 minorVersion = 0;
- SInt32 majorVersion = 0;
- Gestalt(gestaltSystemVersionMajor, &majorVersion);
- Gestalt(gestaltSystemVersionMinor, &minorVersion);
- return majorVersion == 10 && minorVersion >= 7;
- }
- @end
- ................
- IBOutlet NSWindow* _mainWindow; // in h-file
- ................
- - (void) applicationDidFinishLaunching : (NSNotification*) notification
- {
- #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
- if ( [ NSApplication usingLionOSX ] )
- {
- [ [ NSApplication sharedApplication ] setPresentationOptions : NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock ];
- NSWindowCollectionBehavior collection = [ _mainWindow collectionBehavior ];
- collection |= NSWindowCollectionBehaviorFullScreenPrimary;
- [ _mainWindow setCollectionBehavior : collection ];
- [ _mainWindow toggleFullScreen : self ];
- }
- #endif
- }
среда, 20 июля 2011 г.
NSImage Exif (metadata)
Get metadata from NSImage
.........................................
LOG
- + (NSDictionary*) exif : (NSString*) file
- {
- NSDictionary* dic = nil;
- NSURL* url = [ NSURL fileURLWithPath : file ];
- if ( url )
- {
- CGImageSourceRef source = CGImageSourceCreateWithURL ( (CFURLRef) url, NULL);
- if ( NULL == source )
- {
- #ifdef _DEBUG
- CGImageSourceStatus status = CGImageSourceGetStatus ( source );
- NSLog ( @"Error: file name : %@ - Status: %d", file, status );
- #endif
- }
- else
- {
- CFDictionaryRef metadataRef =
- CGImageSourceCopyPropertiesAtIndex ( source, 0, NULL );
- if ( metadataRef )
- {
- NSDictionary* immutableMetadata = (NSDictionary *)metadataRef;
- if ( immutableMetadata )
- {
- dic =
- [ NSDictionary dictionaryWithDictionary : (NSDictionary *)metadataRef ];
- }
- CFRelease ( metadataRef );
- }
- CFRelease(source);
- source = nil;
- }
- }
- return dic;
- }
.........................................
- NSDictionary* dic = [ User_ImageLoader exif : filename ];
- if ( dic )
- {
- NSString* s = [ dic valueForKey : @"Orientation" ];
- NSLog(@"Image : %@ - orentation : %d", filename, [ s intValue ] );
- }
LOG
2011-07-21 00:09:44.579 test.app [12105:7f03] Image : /Users/alexey/Desktop/Works/EXIF Orientation Sample Images/7.jpg - orentation : 7
2011-07-21 00:09:45.908 test.app [12105:7f03] Image : /Users/alexey/Desktop/Works/EXIF Orientation Sample Images/8.jpg - orentation : 8
Create CGImageRef with EXIF Orientation
Create CGImageRef with EXIF Orientation
воскресенье, 19 июня 2011 г.
NSMutableDictionary to NSUserDefaults
- NSMutableDictionary* dic = [ [ NSMutableDictionary alloc ] initWithObjectsAndKeys :
- @"500", @"MaxLoadedImages",
- @"10", @"RequestMaxImages",
- nil ];
- if ( dic )
- {
- [ [ NSUserDefaults standardUserDefaults ] setObject : dic forKey : @"flickr.com" ];
- [ dic release ];
- }
.............................
- NSDictionary* settings = [ [ NSUserDefaults standardUserDefaults ] objectForKey : @"flickr.com" ];
- NSInteger maxLoadedImages = [ [ settings objectForKey : @"MaxLoadedImages" ] integerValue ];
- NSInteger requestMaxImages = [ [ settings objectForKey : @"RequestMaxImages" ] integerValue ];
- NSLog(@"settings : %@", settings);
LOG
MaxLoadedImages = 500;
RequestMaxImages = 10;
}
суббота, 20 ноября 2010 г.
Get resource from bundle screensaver
- NSBundle* bundle = [ NSBundle bundleForClass : [ class_id class ] ];
- // class_id - self gui
- NSString* fileName = [ bundle pathForResource:@"logo" ofType:@"png" ];
вторник, 7 сентября 2010 г.
NSString to wstring
NSString to std::wstring & std::wstring to NSString
.
- std::wstring NSStringToStringW ( NSString* Str )
- {
- NSStringEncoding pEncode = CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );
- NSData* pSData = [ Str dataUsingEncoding : pEncode ];
- return std::wstring ( (wchar_t*) [ pSData bytes ], [ pSData length] / sizeof ( wchar_t ) );
- }
- NSString* StringWToNSString ( const std::wstring& Str )
- {
- NSString* pString = [ [ NSString alloc ]
- initWithBytes : (char*)Str.data()
- length : Str.size() * sizeof(wchar_t)
- encoding : CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE ) ];
- return pString;
- }
Подписаться на:
Сообщения (Atom)