Posts

Showing posts from 2011

Cropping a part of UIImage in iPhone

To crop any part of an image in iPhone using Objective C, start with an UIImage object.    UIImage *image; Create a bitmap representation the image using the CGImageRef class.    CGImageRef imageReference = image.CGImage; Define the frame size for the part of image which should be cropped.    CGRect cropFrame = CGRectMake(10, 10, 50, 50); Now set the current context of UIGraphics    CGContextRef context = UIGraphicsGetCurrentContext(); Draw the image using the crop frame and imageReference using the current context.    CGContextDrawImage(UIGraphicsGetCurrentContext(), cropFrame, imageReference); Save the cropped image on a new UIImage object.    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); After consolidating the above code to a single shot, it looks like,    UIImage *image;    CGImageRef imageReference = image.CGImage;    CGRect cropFrame = CGRectMake(10, 10, 50, 50);    CGContextRef context = UIGraphicsGe

Multiple line of text in UIPickerView

UIPickerView is a user interface component available from iOS 2.0 and above. The basic need of UIPickerView is to select any one option from the list of data that are loaded to the picker view. But, by default, only one line of text is allowed to be displayed in a UIPickerView . Today, we will try to add multi-line text to an UIPickerView . To do so, we need to implement the viewForComponent api.   - ( UIView  *)pickerView:( UIPickerView  *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:( UIView  *)view; In that method, type cast the view parameter to an UILabel.    UILabel *pickerLabel = ( UILabel *)view; Update the frame size of the label.    CGRect frame = CGRectMake ( 0 , 0 , 265 , 40 );    pickerLabel = [[[ UILabel alloc ] initWithFrame :frame] autorelease ]; Set the properties of the label as per requirement.    [pickerLabel setTextAlignment : UITextAlignmentLeft ];    [pickerLabel setBackgro

Play a video using an array of images in iPhone Objective C

When you want to play a video using an array of images in iPhone, you need to run the array of images in a loop and assign the images to the UIImageView. UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame]; for (int i = 0; i < [array count]; i++) {       imageView.image = (UIImage *) [array objectAtIndex: i]; }

How to resolve code sign error in XCode

Getting a code sign error has many reasons. Below are the measured steps to be taken after downloading the profile from Developer account and before making the build. 1. When you download a new/updated profile from the developer account, delete the existing profile and then add the new profile to the "Provisioning Profile" folder. 2. Restart XCode. Since only after restart, the XCode can identify your new profiles. Else it will be referring to your old profiles. 3. Go to Edit Project Settings -> Build Settings -> Code Signing Identity -> (Release/Distribution) -> Other.  Now, clear the old data displayed. Now select the new profile from the list of profiles in Code Signing Identity. The new profile will be displayed at the top of the list, next to "Don't code sign". 4. Repeat Step 3 process in Edit Target Settings. 5. Build and Run. And, ONE MORE THING, Don't forget to Clean All the Targets before making the Build and Run.

Push notification count in iPhone application icon

If you want to show the number of push notifications received by your application from APNS, use the following code.   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];  it will display 1 on the application icon. And, ONE MORE THING, You can also reset the application badge value by setting 0.   [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Dynamic width and height for UILabel in iPhone

Having dynamic text to be displayed in application is a basic requirement. When you want to display the text, we can calculate the size of the dynamic text using the below code.   CGSize dynamicSize = [ sizeWithFont:[UIFont boldSystemFontOfSize:14.0] constrainedToSize:CGSizeMake(320, 999) lineBreakMode:UILineBreakModeWordWrap]; The font, constrainedToSize and lineBreakMode should be updated based on the requirement. Now, dynamic height value can be derived like,   NSLog(@"Dynamic Text Width: %f \nHeight: %f", dynamicSize.width, dynamicSize.height); And, to update the UILabel, use the dynamicSize.height value,   UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,dynamicSize.width, dynamicSize.height)]; And, ONE MORE THING, Don't forget to set the numberOfLines property of UILabel to 0.