This article is shared by members in the twilight Music Festival
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. _ % +-] [email protected] [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];
Community post: http://www.cocoachina.com/bbs/read.php? Tid = 108916 & page = 1
17 common codes