Показаны сообщения с ярлыком Cocoa. Показать все сообщения
Показаны сообщения с ярлыком Cocoa. Показать все сообщения

вторник, 11 сентября 2012 г.

Create CGImageRef with EXIF Orientation

  1. static inline double rad (int alpha)  
  2. {  
  3.     return ((alpha * pi)/180);  
  4. }  
  5.   
  6. static CGImageRef RotateImageWithFlip (CGImageRef source, float angle, 
  7.                                    BOOL flipX, BOOL flipY)  
  8. {  
  9.     float fX    =  fabs ( cos ( rad ( angle ) ) );  
  10.     float fY    =  fabs ( sin ( rad ( angle ) ) );  
  11.       
  12.     float dW    =  (float)CGImageGetWidth  (source) * fX +
  13.                     (float)CGImageGetHeight (source) * fY;  
  14.     float dH    =  (float)CGImageGetWidth  (source) * fY +
  15.                     (float)CGImageGetHeight (source) * fX;  
  16.       
  17.     CGContextRef context = CGBitmapContextCreate (NULL,  
  18.                                           (size_t)dW, (size_t)dH,   
  19.                                           CGImageGetBitsPerComponent(source),   
  20.                                           0,   
  21.                                           CGImageGetColorSpace(source),  
  22.                                           kCGImageAlphaPremultipliedLast);  
  23.       
  24.     CGContextSetAllowsAntialiasing(context, NO);  
  25.     CGContextSetShouldAntialias(context, NO);  
  26.     CGContextSetInterpolationQuality (context, kCGInterpolationLow);  
  27.       
  28.     CGAffineTransform transform =
  29.     CGAffineTransformMakeTranslation(flipX?dW:0.0f,flipY?dH:0.0f);  
  30.     transform = CGAffineTransformScale (transform,flipX?-1.0:1.0f,flipY?-1.0: 1.0f);  
  31.   
  32.     if (0.0f != angle)  
  33.     {  
  34.         CGAffineTransform rot = CGAffineTransformMakeTranslation (dW*0.5f,dH*0.5f);  
  35.         rot                   = CGAffineTransformRotate(rot, rad ( angle ));  
  36.         rot                   = CGAffineTransformTranslate (rot,-dH*0.5f,-dW*0.5f);  
  37.         transform             = CGAffineTransformConcat(rot, transform);  
  38.     }  
  39.       
  40.     CGContextConcatCTM(context, transform);  
  41.       
  42.     CGContextDrawImage(context, CGRectMake (0, 0, CGImageGetWidth (source),   
  43.                                                     CGImageGetHeight (source)), source);      
  44.     CGContextFlush(context);      
  45.     CGImageRef rotated  =   CGBitmapContextCreateImage(context);  
  46.     CGContextRelease(context);  
  47.   
  48.     return rotated;  
  49. }  

  50. static CGImageRef RotateWithEXIF (CGImageRef source, int orientation)  
  51. {  
  52.     switch (orientation)  
  53.     {  
  54.         case 2:  
  55.             return RotateImageWithFlip (source, 0, YES, NO);  
  56.             break;  
  57.         case 3:  
  58.             return RotateImageWithFlip (source, 0, YES, YES);  
  59.             break;  
  60.         case 4:  
  61.             return RotateImageWithFlip (source, 0, NO, YES);  
  62.             break;  
  63.   
  64.         case 5:  
  65.             return RotateImageWithFlip (source, 90, NO, YES);  
  66.             break;  
  67.         case 6:  
  68.             return RotateImageWithFlip (source, -90, NO, NO);  
  69.             break;  
  70.               
  71.         case 7:  
  72.             return RotateImageWithFlip (source, 90, YES, NO);  
  73.             break;  
  74.         case 8:  
  75.             return RotateImageWithFlip (source, -90, YES, YES);  
  76.             break;  
  77.               
  78.         default:  
  79.             break;  
  80.     }  
  81.       
  82.     return NULL;  
  83. }  

вторник, 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.

If the application is actively using OpenGL.

When initializing the NS*View is bound to 3D-context, do check the retina.

  1. #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7  
  2.         if ( [ NSApplication usingLionOSX ] )  
  3.         {  
  4.           NSRect baseBounds = [ self bounds ];  
  5.           NSRect hdpiBounds = [ self convertRectToBacking : baseBounds ];  
  6.            
  7.           if (NO == NSEqualSizes(baseBounds.size, hdpiBounds.size))  
  8.           {            
  9.               [ self setWantsBestResolutionOpenGLSurface : YES ];  
  10.           }  
  11.         }  
  12. #endif  

If you change the size of the application recalculate dimensions.

  1. - (void) updateBounds  
  2. {  
  3.     NSRect  baseBounds  =   [ self bounds ];  
  4.       
  5. #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7  
  6.     if ( [ NSApplication usingLionOSX ] )  
  7.     {        
  8.         baseBounds      =   [ self convertRectToBacking : baseBounds ];  
  9.     }   
  10. #endif  
  11.   ....  
  12.    
  13.   glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f );   
  14.   glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );   
  15.   glViewport (0.0f, 0.0f, baseBounds.size.width, baseBounds.size.height );  
  16.   ....  
  17.   
  18. }  

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.

  1. NSImageRep* rep = [[img representations] objectAtIndex:0];    // fix HDPI  
  2. if (rep)  
  3. {  
  4.        NSSize size = NSMakeSize ([rep pixelsWide], [rep pixelsHigh]);  
  5.        [img setSize : size];  
  6. }  


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.

  1. + (CTFontRef) fontFromBundle : (NSString*) fontName withHeight : (CGFloat) height;  
  2. {  
  3.     // Get the path to our custom font and create a data provider.  
  4.     NSString* fontPath = [[NSBundle mainBundle] pathForResource : fontName
  5.                                                          ofType : @"ttf" ];   
  6.     if (nil==fontPath)  
  7.         return NULL;  
  8.       
  9.     CGDataProviderRef dataProvider =
  10.     CGDataProviderCreateWithFilename ([fontPath UTF8String]);  
  11.     if (NULL==dataProvider)  
  12.         return NULL;  
  13.       
  14.     // Create the font with the data provider, then release the data provider.  
  15.     CGFontRef fontRef = CGFontCreateWithDataProvider ( dataProvider );  
  16.     if ( NULL == fontRef )  
  17.     {  
  18.         CGDataProviderRelease ( dataProvider );   
  19.         return NULL;  
  20.     }      
  21.       
  22.     CTFontRef fontCore = CTFontCreateWithGraphicsFont(fontRef, height, NULL, NULL);  
  23.     CGDataProviderRelease (dataProvider);   
  24.     CGFontRelease(fontRef);  
  25.       
  26.     return fontCore;  
  27. }  

In code

  1. CTFontRef bundleFont = [ CustomFonts fontFromBundle : @"MavenPro-Regular"
  2.                                          withHeight : 25 ];  
  3. NSFont* font = (NSFont*)bundleFont;  
  4. ....  
  5. // use NSFont   
  6. ....  
  7. CFRelease(bundleFont);  
  8.    

UPD. works correctly only 10.7+

понедельник, 5 сентября 2011 г.

Manually set the full-screen mode at startup application in Lion 10.7

  1. ................  
  2.  
  3. #import <CoreServices/CoreServices.h>  
  4.   
  5. @implementation NSApplication (SystemVersion)  
  6.   
  7. + (BOOL) usingLionOSX  
  8. {  
  9.     SInt32 minorVersion = 0;  
  10.     SInt32 majorVersion = 0;  
  11.       
  12.     Gestalt(gestaltSystemVersionMajor, &majorVersion);  
  13.     Gestalt(gestaltSystemVersionMinor, &minorVersion);  
  14.       
  15.     return majorVersion == 10 && minorVersion >= 7;      
  16. }  
  17.   
  18. @end  
  19.   
  20. ................  
  21.   
  22. IBOutlet NSWindow*  _mainWindow;    //  in h-file  
  23.   
  24. ................  
  25.   
  26. - (void) applicationDidFinishLaunching : (NSNotification*) notification  
  27. {         
  28. #if (defined(MAC_OS_X_VERSION_10_7)) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7  
  29.       
  30.     if ( [ NSApplication usingLionOSX ] )  
  31.     {  
  32.         [ [ NSApplication sharedApplication ] setPresentationOptions : NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationAutoHideDock ];  
  33.           
  34.         NSWindowCollectionBehavior collection = [ _mainWindow collectionBehavior ];  
  35.         collection |= NSWindowCollectionBehaviorFullScreenPrimary;  
  36.          
  37.         [ _mainWindow setCollectionBehavior : collection ];  
  38.         [ _mainWindow toggleFullScreen : self ];  
  39.     }  
  40.      
  41. #endif  
  42. }  

среда, 20 июля 2011 г.

NSImage Exif (metadata)

Get metadata from NSImage

  1. + (NSDictionary*) exif : (NSString*) file  
  2. {  
  3.     NSDictionary* dic   =   nil;  
  4.       
  5.     NSURL* url          =   [ NSURL fileURLWithPath : file ];  
  6.       
  7.     if ( url )  
  8.     {  
  9.         CGImageSourceRef source = CGImageSourceCreateWithURL ( (CFURLRef) url, NULL);  
  10.           
  11.         if ( NULL == source )   
  12.         {  
  13. #ifdef _DEBUG  
  14.             CGImageSourceStatus status = CGImageSourceGetStatus ( source );  
  15.             NSLog ( @"Error: file name : %@ - Status: %d", file, status );  
  16. #endif            
  17.         }  
  18.         else  
  19.         {             
  20.             CFDictionaryRef metadataRef = 
  21.             CGImageSourceCopyPropertiesAtIndex ( source, 0, NULL );  
  22.             if ( metadataRef )   
  23.             {  
  24.                 NSDictionary* immutableMetadata = (NSDictionary *)metadataRef;  
  25.                 if ( immutableMetadata )  
  26.                 {                 
  27.                     dic =   
  28.                 [ NSDictionary dictionaryWithDictionary : (NSDictionary *)metadataRef ];  
  29.                 }                                          
  30.                   
  31.                 CFRelease ( metadataRef );  
  32.             }  
  33.               
  34.             CFRelease(source);  
  35.             source = nil;  
  36.         }  
  37.     }  
  38.       
  39.     return dic;  
  40. }  

.........................................

  1. NSDictionary* dic = [ User_ImageLoader exif : filename ];  
  2. if ( dic )  
  3. {  
  4.     NSString* s     =   [ dic valueForKey : @"Orientation" ];  
  5.       
  6.     NSLog(@"Image : %@ - orentation : %d", filename, [ s intValue ] );  
  7. }  

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

воскресенье, 19 июня 2011 г.

NSMutableDictionary to NSUserDefaults

  1. NSMutableDictionary* dic = [ [ NSMutableDictionary alloc ] initWithObjectsAndKeys :   
  2.                             @"500", @"MaxLoadedImages",   
  3.                             @"10",  @"RequestMaxImages",  
  4.                             nil ];   
  5. if ( dic )  
  6. {         
  7.     [ [ NSUserDefaults standardUserDefaults ] setObject : dic forKey : @"flickr.com" ];       
  8.     [ dic release ];  
  9. }  

.............................

  1. NSDictionary* settings          =   [ [ NSUserDefaults standardUserDefaults ] objectForKey : @"flickr.com" ];  
  2. NSInteger maxLoadedImages       =   [ [ settings objectForKey : @"MaxLoadedImages" ] integerValue ];  
  3. NSInteger requestMaxImages      =   [ [ settings objectForKey : @"RequestMaxImages" ] integerValue ];  
  4.   
  5. NSLog(@"settings : %@", settings);  

LOG

2011-06-19 14:07:34.600 test.app[3464:6b03] settings : {
MaxLoadedImages = 500;
RequestMaxImages = 10;
}

суббота, 20 ноября 2010 г.

Get resource from bundle screensaver

  1. NSBundle* bundle = [ NSBundle bundleForClass : [ class_id class ] ];    
  2. // class_id - self gui    
  3. NSString* fileName = [ bundle pathForResource:@"logo" ofType:@"png" ];  

вторник, 7 сентября 2010 г.

NSString to wstring

NSString to std::wstring & std::wstring to NSString

  1. std::wstring NSStringToStringW ( NSString* Str )   
  2. {   
  3.     NSStringEncoding pEncode    =   CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );   
  4.     NSData* pSData              =   [ Str dataUsingEncoding : pEncode ];    
  5.    
  6.     return std::wstring ( (wchar_t*) [ pSData bytes ], [ pSData length] / sizeof ( wchar_t ) );   
  7. }   
  8.    
  9. NSString* StringWToNSString ( const std::wstring& Str )   
  10. {   
  11.     NSString* pString = [ [ NSString alloc ]    
  12.                         initWithBytes : (char*)Str.data()   
  13.                                length : Str.size() * sizeof(wchar_t)   
  14.                              encoding : CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE ) ];   
  15.     return pString;   
  16. }  
.