1. If you want to process an image in a program (to obtain a part of an image), you can use the following code:
UIImage * image = [UIImage imageNamed: filename];
CGImageRef imageRef = image. CGImage;
CGRect rect = CGRectMake (origin. x, origin. y, size. width, size. height );
CGImageRef imageRefRect = CGImageCreateWithImageInRect (imageRef, rect );
UIImage * imageRect = [[UIImage alloc] initWithCGImage: imageRefRect];
2. code used to determine whether the device is an iphone or iphone 4:
# Define isRetina ([UIScreen instancesRespondToSelector: @ selector (currentMode)]? CGSizeEqualToSize (CGSizeMake (640,960), [[UIScreen mainScreen] currentMode]. size): NO)
3. Check whether the email address is entered correctly:
-(BOOL) validateEmail: (NSString *) candidate {
NSString * emailRegex = @ "[A-Z0-9a-z. _ % +-] + @ [A-Za-z0-9.-] + \. [A-Za-z] {2, 4 }";
NSPredicate * emailTest = [NSPredicate predicateWithFormat: @ "self matches % @", emailRegex];
Return [emailTest evaluateWithObject: candidate];
}
4. How to save the current view as a photo album:
# Import <QuartzCore/QuartzCore. h>
UIGraphicsBeginImageContext (currentView. bounds. size); // currentView current view
[CurrentView. layer renderInContext: UIGraphicsGetCurrentContext ()];
UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
UIImageWriteToSavedPhotosAlbum (viewImage, nil );
5. A local notification (similar to a push notification) is triggered 10 seconds after the home key is pressed to the background:
UILocalNotification * notification = [[UILocalNotification alloc] init];
If (notification! = Nil ){
NSLog (@ "> support local notification ");
NSDate * now = [NSDate new];
Notification. fireDate = [now addTimeInterval: 10];
Notification. timeZone = [NSTimeZone defaultTimeZone];
Notification. alertBody = @ "it's time to eat dinner! ";
[[UIApplication sharedApplication]. scheduleLocalNotification: notification];
}
6. Capture iphone call events:
CTCallCenter * center = [[CTCallCenter alloc] init];
Center. callEventHandler = ^ (CTCall * call)
{
NSLog (@ "call: % @", call. callState );
}
7. iOS 4 introduces multi-task support, so after you press the "Home" key, the program may not exit but be transferred to the background for running. If you want the Application to exit directly, the simplest method is to find the Application does not run in background option in info-plist, and select this option.
8. Rotate the image of UIimageView:
Float rotateAngle = M_PI;
CGAffineTransform transform = CGAffineTransformMakeRotation (rotateAngle );
ImageView. transform = transform;
9. Set the rotation origin:
# Import <QuartzCore/QuartzCore. h>
UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "bg.png"];
ImageView. layer. anchorPoint = CGPointMake (0.5, 1.0 );
10. Implement a custom Status Bar (covering the status bar ):
CGRect frame = {0, 0 },{ 320, 20 }};
UIWindow * wd = [[UIWindow alloc] initWithFrame: frame];
[Wd setBackgroundColor: [UIColor clearColor];
[Wd setWindowLevel: UIWindowLevelStatusBar];
Frame = CGRectMake (100, 0, 30, 20 );
UIImageView * img = [[UIImageView alloc] initWithFrame: frame];
[Img setContentMode: UIViewContentModeCenter];
[Img setImage: [UIImage imageNamed: @ "00_0103.png"];
[Wd addSubview: img];
[Wd makeKeyAndVisible];
[UIView beginAnimations: nil context: nil];
[UIView setAnimationDuration: 2];
Frame. origin. x + = 150;
[Img setFrame: frame];
[UIView commitAnimations];
11. Call in the program:
// Add the phone icon button
UIButton * btnPhone = [[UIButton buttonWithType: UIButtonTypeCustom] retain];
BtnPhone. frame = CGRectMake (280,10, 30,30 );
UIImage * image = [UIImage imageNamed: @ "phone.png"];
[BtnPhone setBackgroundImage: image forState: UIControlStateNormal];
// Click the dialing button to directly dial
[BtnPhone addTarget: self action: @ selector (callAction: event :) forControlEvents: UIControlEventTouchUpInside];
[Cell. contentView addSubview: btnPhone]; // cell is a UITableViewCell
// Define the operation when the dial button is clicked
-(Void) callAction :( id) sender event :( id) event {
NSSet * touches = [event allTouches];
UITouch * touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView: self. listTable];
NSIndexPath * indexPath = [self. listTable indexPathForRowAtPoint: currentTouchPosition];
If (indexPath = nil ){
Return;
}
NSInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSDictionary * rowData = [datas objectAtIndex: row];
NSString * num = [[NSString alloc] initWithFormat: @ "tel: // % @", number]; // number indicates the number string
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: num]; // dial
}
12. Change the iphone keyboard color:
1. Only these two types of numeric keyboards have an effect. UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad
2. keyboardAppearance = UIKeyboardAppearanceAlert
-(Void) textViewDidBeginEditing :( UITextView *) textView {
NSArray * ws = [[UIApplication sharedApplication] windows];
For (UIView * w in ws ){
NSArray * vs = [w subviews];
For (UIView * v in)
{
If ([[NSString stringwithuf8string: object_getClassName (v)] isEqualToString: @ "UIKeyboard"])
{
V. backgroundColor = [UIColor redColor];
}
}
}
13. Set the time zone
NSTimeZone * defaultTimeZone = [NSTimeZone defaultTimeZone];
NSTimeZone * tzGMT = [NSTimeZone timeZoneWithName: @ "GMT"];
[NSTimeZone setdefatimetimezone: tzGMT];
Use either of the preceding two time zones.
14. How to hide and trigger a keyboard on the Ipad.
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (keyboardWillHide :)
Name: UIKeyboardWillHideNotification
Object: nil];
-(IBAction) keyboardWillHide :( NSNotification *) note
14. How to open another program in one program.
Http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
15. Calculate the number of characters in a string
-(Int) calculateTextNumber :( NSString *) text
{
Float number = 0.0;
Int index = 0;
For (index; index <[text length]; index ++)
{
NSString * protoText = [text substringToIndex: [text length]-index];
NSString * toChangetext = [text substringToIndex: [text length]-1-index];
NSString * charater;
If ([toChangetext length] = 0)
{
Charater = protoText;
}
Else
{
Nsange range = [text rangeOfString: toChangetext];
Charater = [protoText stringByReplacingCharactersInRange: range withString: @ ""];
}
NSLog (charater );
If ([charater lengthOfBytesUsingEncoding: NSUTF8StringEncoding] = 3)
{
Number ++;
}
Else
{
Number = number + 0.5;
}
}
Return ceil (number );
}