G8Production

Su di me

G8Production sito ufficiale di Daniele Galiotto.
Freelancer Senior iOS Apple Developer.
Programmatore Senior iOS, vivo in Umbria, tra Roma e Perugia. Sviluppo con Objective-C ed XCode da molto tempo.
In questo sito troverai informazioni tecniche di programmazione, docenze, iPhone, Mac, Objective-C, iOS....

Play sound in iOS apps: AudioToolbox

How to play (short) sounds in iOS apps

The max length of the sound is 30”. Don’t forget this.

Well, suppose that you have a “backtothefuture.mp3” file into the iOS app project.

Now you can follow this simple steps:

1. add the AudioToolbox.framework

2. import the AudioToolbox.framework writing

#import <AudioToolbox/AudioToolbox.h>

3. write the code for create, play and dispose the sound using the AudioServices classes

+(void)playSound
{
                NSString *songName = @"backtothefuture.mp3";
		SystemSoundID mSound;
		
		NSString* path = [[NSBundle mainBundle]
					pathForResource:songName ofType:nil];
		NSURL* url = [NSURL fileURLWithPath:path];
		
		AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &mSound);
		
		// Sounds played with AudioServicesPlaySystemSound can not be longer then 30 secs.
		// So we can dispose the system sound id after that period.
		AudioServicesAddSystemSoundCompletion(mSound, NULL, NULL, systemAudioCallback, NULL);
		AudioServicesPlaySystemSound(mSound);
}

//it is a function, not a method! This function is called when the sound stops to play
void systemAudioCallback(SystemSoundID soundId, void *clientData)
{
	AudioServicesDisposeSystemSoundID(soundId);
}

4. finally call the class method writing [MySoundClass playSound];

That’s all!

1 nota ios AudioServicesPlaySystemSound apple sound audiotoolbox

How to change the Anchor Point of UIView programmatically

How to change the Anchor Point of UIView programmatically | www.g8production.com | Daniele Galiotto

The (origin) anchor point is a important part of a UIView. You use it anytime that you will rotate, scale, and so on… a UIView.

If you wanna change the anchor point of UIView programmatically, you must follow only two steps:

just add the QuartzCore framework into your project and import it in your Objective-C class:

#import <QuartzCore/QuartzCore.h>

set the anchor point for the UIView layer:

[myView.layer setAnchorPoint:CGPointMake(1f, 1f)];

That’s all.

apple anchor point ios objective-c

How to crop image in iOS

The best way to crop a image in iOS is writing the code below:

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
UIImage* croppedImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

return croppedImage;

Else, if you wanna grab a part of image, proportionally, you can write (edit it!) the code below

    CGRect rect = cropRect;
	
	CGRect screen = [UIScreen mainScreen].bounds;
	
	float scaleX = self.imageToCrop.size.width / screen.size.width;
	float scaleY = self.imageToCrop.size.height / screen.size.height;
	
	rect.size.width = rect.size.width * scaleX;
	rect.size.height = rect.size.height * scaleY;
	rect.origin.x = rect.origin.x * scaleX;
	rect.origin.y = rect.origin.y * scaleY;
	
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.imageToCrop.size.width, self.imageToCrop.size.height);

	// draw image
	[self.imageToCrop drawInRect:drawRect];
	
	// grab image
	UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
     
     return croppedImage;
	

That’s all!

ios apple crop image uiimage context

How to resize UINavigationController after setStatusBarHidden

[[UIApplication sharedApplication] setStatusBarHidden:YES];

will help you to hide or show the status bar. But when you use a UINavigationController you must update the navigation bar every time you use the setStatusBarHidden method.

Well this is really easy.

In the viewWillAppear method hide and show the navigation bar, like below.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

That’s all!

setStatusBarHidden ios apple navigationbar status bar

Objective-C: Convert number to absolute value

Convert number to its absolute value is possible using the C standard library, available in Objective-C.

There is always a specific function to call and it depends on the type of your variable:

  • abs(int)
  • labs(long)
  • llabs(long long)
  • imaxabs(intmax_t)
  • fabsf(float)
  • fabs(double)
  • fabsl(long double)

That’s all!

apple objective-c absolute

TesseractOCR Framework - OCR Framework for iOS under MIT license

Optical character recognition, usually abbreviated to OCR, is the mechanical or electronic conversion of scanned images of handwritten, typewritten or printed text into machine-encoded text.

Tesseract OCR iOS | www.g8production.com - Daniele Galiotto

Tesseract OCR iOS is a MIT Framework for iOS5+, compiled also for armv7s (iPhone5+). 

Created by g8production.com, it will help you to use OCR in a iOS project. Easy and fast.

 TesseractOCR.framework is available on GitHub at link https://github.com/gali8/Tesseract-OCR-iOS

 

Tesseract OCR iOS and TesseractOCR.framework powered by g8production are under MIT License. 
Tesseract-ios, powered by ldiqual https://github.com/ldiqual/tesseract-ios, is under MIT License. 
Tesseract, powered by Google http://code.google.com/p/tesseract-ocr/, is under Apache License.

ios framework ios apple ocr tesseract ios tesseract ocr framework

G8MKLocalSearch, a MIT class to search in MapKit

G8MKLocalSearch | www.g8production.com - Daniele Galiotto

G8MKLocalSearch is a really simple class, for search addresses, places, cities, etc… using a “normal language” query.

G8MKLocalSearch automatically switch between MKLocalSearchResponse (available from iOS6.1) and NSDictionary (from iOS < 6.1).

It’s released under MIT license.
Copyright Daniele Galiotto :)

You can download G8MKLocalSearch from here (GitHub).

apple ios6.1 ios5 G8MKLocalSearch MKLocalSearch

Objective-C runtime functions (id SEL IMP)

Objective-C Runtime reference functions | www.g8production.com Daniele Galiotto

When you are using the “Objective-C Runtime Reference”, you can use very useful functions.

Below, from the iOS Debugging Magic doc, three good functions for get Class, SEL or IMP (pointer-to-code-with-method).

THE FUNCTIONS:

  • id objc_getClass(const char *name); Get the Objective-C Class object for the given class name
  • SEL sel_getUid(const char *str); Get the Objective-C SEL for the given method name
  • IMP class_getMethodImplementation(Class cls, SEL name); Get a pointer to the code that implements the given method in a given class

That’s all!

ios runtime apple objective-c reference debugging

Resolve the error dyld Library not loaded: AdSupport, image not found.

When you try to use a new framework (ex. AdSupport is available in iOS6 but not in iOS5) in a device with a old iOS version, you will receive, in the console area, a message error like this:

dyld: Library not loaded: /System/Library/Frameworks/AdSupport.framework/AdSupport Referenced from: /var/mobile/Applications/THEAPPID/APPNAME.app/APPNAME Reason: image not found


Solve the problem is easy. Go to the “Build Phases” tab in your target and inside the “Link Binary With Libraries” panel set to Optional the bad frameworks!

For AdSupport, maybe you must set to optional other frameworks too:

  • AdSupport
  • Social
  • Accounts

See the picture below.

Resolve the error dyld Library not loaded: AdSupport, image not found. | www.g8production.com | Daniele Galiotto

ios6 ios5 apple AdSupport framework

Execute the correct code based on the current iOS SDK

Every time there is a new version of iOS, some methods can become deprecated.

So when i write the code i wanna use the correct code for the current iOS installed in the device.

For example i want to execute two different methods in iPad 1 and iPad 2, based on the current installed iOS. The iPad 1 can not support iOS 6; iPad 2, contrarily, it can.

So, some methods are deprecated in iOS6 but available in iOS5; at the same time some methods are available in iOS6 but not supported in iOS5.

The solution is simple, you can use the __IPHONE_OS_VERSION_MIN_REQUIRED macro like below.

#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000

//bla bla bla the iOS < 6 code
#else
//bla bla bla the iOS >= 6 code
#endif

That’s all!

ios6 ios apple