1. check whether this object or method exists
ResponseToSelector: @ selector
2. Delayed asynchronous execution
PeformSelector: withObject: afterDelay
3. simulator path:
/[User]/Library/Application Support/iPhone Simulator/4.2/Applications/B0E3EC07-F395-408F-9B94-A5B0BAECBC2E/Documents/personal. plist
4. Custom send notification
Send message
NSDictionary * info = [[NSDictionaryalloc] initWithObjectsAndKeys: nil, @ "ret", nil];
[[Nsnotifcencenterdefacenter] postNotificationOnMainThreadWithName: @ "LoginSuccessful" object: self userInfo: info];
Receive messages
[[Nsnotifcencenterdefacenter] addObserver: self selector: @ selector (loginSuccessful :) name: @ "LoginSuccessful" object: nil];
1. code used to determine whether the email format is correct
// Use regular expression for verification
-(BOOL) isValidateEmail :( NSString *) email
{
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: email];
}
2. Image Compression
Usage: UIImage * yourImage = [self imageWithImageSimple: image scaledToSize: CGSizeMake (210.0, 210.0)];
// Compress the image
-(UIImage *) imageWithImageSimple :( UIImage *) image scaledToSize :( CGSize) newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext (newSize );
// Tell the old image to draw in this newcontext, with the desired
// New size
[Image drawInRect: CGRectMake (0, 0, newSize. width, newSize. height)];
// Get the new image from the context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
// End the context
UIGraphicsEndImageContext ();
// Return the new image.
Return newImage;
}
3. Test the available image upload code.
-(IBAction) uploadButton :( id) sender {
UIImage * image = [UIImage imageNamed: @ "1.jpg"]; // image name
NSData * imageData = UIImageJPEGRepresentation (image, 0.5); // compression ratio
NSLog (@ "Bytes: % I", [imageData length]);
// Post url
NSString * urlString = @ "http: // 192.168.1.113: 8090/text/UploadServlet ";
// Server address
// Setting up the request object now
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[Request setURL: [NSURL URLWithString: urlString];
[Request setHTTPMethod: @ "POST"];
//
NSString * boundary = [NSString stringWithString: @ "--------------------------- 14737809831466499882746641449"];
NSString * contentType = [NSString stringWithFormat: @ "multipart/form-data; boundary = % @", boundary];
[Request addValue: contentType forHTTPHeaderField: @ "Content-Type"];
//
NSMutableData * body = [NSMutableData data];
[Body appendData: [[NSString stringWithFormat: @ "\ r \ n -- % @ \ r \ n", boundary] dataUsingEncoding: NSUTF8StringEncoding];
[Body appendData: [[NSString stringWithString: @ "Content-Disposition: form-data; name = \" userfile \"; filename = \ "2.png \" \ r \ n "] dataUsingEncoding: NSUTF8StringEncoding]; // name of the uploaded Image
[Body appendData: [[NSString stringWithString: @ "Content-Type: application/octet-stream \ r \ n"] dataUsingEncoding: NSUTF8StringEncoding];
[Body appendData: [NSData dataWithData: imageData];
[Body appendData: [[NSString stringWithFormat: @ "\ r \ n -- % @ -- \ r \ n", boundary] dataUsingEncoding: NSUTF8StringEncoding];
[Request setHTTPBody: body];
// NSLog (@ "1-body: % @", body );
NSLog (@ "2-request: % @", request );
NSData * returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString * returnString = [[NSString alloc] initWithData: returnData encoding: NSUTF8StringEncoding];
NSLog (@ "3-Test output: % @", returnString );
4. load images to imageView
UIImage * myImage = [UIImage imageNamed: @ "1.jpg"];
[ImageView setImage: myImage];
[Self. view addSubview: imageView];
5. operations on the Image Library
Select album:
UIImagePickerControllerSourceTypesourceType = UIImagePickerControllerSourceTypeCamera;
If (! [UIImagePickerControllerisSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
SourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController * picker = [[UIImagePickerControlleralloc] init];
Picker. delegate = self;
Picker. allowsEditing = YES;
Picker. sourceType = sourceType;
[Self presentModalViewController: picker animated: YES];
Selected:
-(Void) imagePickerController :( UIImagePickerController *) pickerdidFinishPickingMediaWithInfo :( NSDictionary *) info
{
[Picker dismissModalViewControllerAnimated: YES];
UIImage * image = [info objectForKey: UIImagePickerControllerEditedImage];
[Self defined mselector: @ selector (selectPic :) withObject: imageafterDelay: 0.1];
}
-(Void) selectPic :( UIImage *) image
{
NSLog (@ "image % @", image );
ImageView = [[UIImageView alloc] initWithImage: image];
ImageView. frame = CGRectMake (0, 0, image. size. width, image. size. height );
[Self. viewaddSubview: imageView];
[Self validate mselectorinbackground: @ selector (detect :) withObject: nil];
}
Detect is a method defined by the user. The effect to be achieved after the photo is edited and selected
Unselect:
-(Void) imagePickerControllerDIdCancel :( UIImagePickerController *) picker
{
[Picker dismissModalViewControllerAnimated: YES];
}
6. jump to the next View
NextWebView = [[WEBViewController alloc] initWithNibName: @ "WEBViewController" bundle: nil];
[Self presentModalViewController: nextWebView animated: YES];
// Create a button on the Right of UIBarButtonItem
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithTitle: @ "right" style: UIBarButtonItemStyleDone target: self action: @ selector (clickRightButton)];
[Self. navigationItem setRightBarButtonItem: rightButton];
Set navigationBar to hide
Self. navigationController. navigationBarHidden = YES ;//
Automatic line feed (automatic LINE folding) for multiple lines of UIlabel in iOS development)
UIView * footerView = [[UIView alloc] initWithFrame: CGRectMake (10,100,300,180)];
UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (10,100,300,150)];
Label. text = @ "Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld! ";
// The background color is red.
Label. backgroundColor = [UIColor redColor];
// Set the font color to white
Label. textColor = [UIColor whiteColor];
// Text center display
Label. textAlignment = UITextAlignmentCenter;
// Automatic LINE folding settings
Label. lineBreakMode = UILineBreakModeWordWrap;
Label. numberOfLines = 0;
7. Code Generation button
CGRect frame = CGRectMake (0,400, 72.0, 37.0 );
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = frame;
[Button setTitle: @ "New button" forState: UIControlStateNormal];
Button. backgroundColor = [UIColor clearColor];
Buttons. tag = 2000;
[Button addTarget: self action: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: button];
8. display a widget in the center of the View
(A control, such as label and View) label. center = self. view. center;
9. Nice-looking text processing
Take the cell textLabel in tableView as an example:
Cell. backgroundColor = [UIColorscrollViewTexturedBackgroundColor];
// Set the text font
Cell. textLabel. font = [UIFont fontWithName: @ "AmericanTypewriter" size: 100366f];
// Set the text color
Cell. textLabel. textColor = [UIColor orangeColor];
// Set the background color of the text
Cell. textLabel. shadowColor = [UIColor whiteColor];
// Set the text display position
Cell. textLabel. textAlignment = UITextAlignmentCenter;
10. Hide the Status Bar
The reader may know a simple method, that is, adding
[[UIApplication sharedApplication] setStatusBarHidden: YES animated: NO];
11. Change the AlertView background
UIAlertView * theAlert = [[UIAlertViewalloc] initWithTitle: @ "Atention"
Message: @ "I'm a Chinese! "
Delegate: nil
CancelButtonTitle: @ "Cancel"
OtherButtonTitles: @ "Okay", nil] autorelease];
[TheAlert show];
UIImage * theImage = [UIImageimageNamed: @ "loveChina.png"];
TheImage = [theImage stretchableImageWithLeftCapWidth: 0 topCapHeight: 0];
CGSize theSize = [theAlert frame]. size;
UIGraphicsBeginImageContext (theSize );
[TheImage drawInRect: CGRectMake (5, 5, theSize. width-10, theSize. height-20)]; // adjust the size of this place to adapt to the background color of alertview.
TheImage = UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
TheAlert. layer. contents = (id) [theImage CGImage];
12. Transparent keyboard
TextField. keyboardAppearance = UIKeyboardAppearanceAlert;
Indicates whether the network active firewheel in the status bar is rotated.
[UIApplication sharedApplication]. networkActivityIndicatorVisible. The default value is NO.
13. capture screen images
// Create a bitmap-based image context and specify the size as CGSizeMake (200,400)
UIGraphicsBeginImageContext (CGSizeMake (200,400 ));
// RenderInContext presents the receiver and Its subrange to the specified context
[Self. view. layer renderInContext: UIGraphicsGetCurrentContext ()];
// Returns an image based on the current image context.
UIImage * aImage = UIGraphicsGetImageFromCurrentImageContext ();
// Remove the image context at the top of the stack based on the current bitmap
UIGraphicsEndImageContext ();
// Return the data of the specified image in png format
ImageData = UIImagePNGRepresentation (aImage );
14. Change the background of the cell.
UIView * myview = [[UIView alloc] init];
Myview. frame = cgrecctmake (0, 0,320, 47 );
Myview. backgroundColor = [UIColorcolorWithPatternImage: [UIImage imageNamed: @ "0006.png"];
Cell. selectedBackgroundView = myview;
15. display images
CGRect myImageRect = CGRectMake (0.0f, 0.0f, 3200000f, 109.0f );
UIImageView * myImage = [[UIImageView alloc] initWithFrame: myImageRect];
[MyImage setImage: [UIImage imageNamed: @ "myImage.png"];
MyImage. opaque = YES; // whether opaque is transparent
[Self. view addSubview: myImage];
16. Adjust the image size to the frame size without confirmation)
NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "XcodeCrash" ofType: @ "png"];
UIImage * image = [[UIImage alloc] initWithContentsOfFile: imagePath];
UIImage * newImage = [image transformWidth: 80.f height: 240.f];
UIImageView * imageView = [[UIImageView alloc] initWithImage: newImage];
[NewImagerelease];
[Image release];
[Self. view addSubview: imageView];
17. Code for clicking an image to jump: generate a button with a background image and bind the button with the desired event!
UIButton * imgButton = [UIButton alloc] initWithFrame: CGRectMake (0, 0,120,120)];
[ImgButton setBackgroundImage :( UIImage *) [self. imgArray objectAtIndex: indexPath. row] forState: UIControlStateNormal];
ImgButton. tag = [indexPath row];
[ImgButton addTarget: self action: @ selector (buttonClick :) forControlEvents: UIControlEventTouchUpInside];