版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、史上最全的ios源碼匯總合集1.判斷郵箱格式是否正確的代碼: 1. / 利用正則表達式驗證 -( BOOL )isValidateEmail:( NSString *)email 2. 3. NSString *emailRegex = "A-Z0-9a-z._%+-+A-Za-z0-9.-+.A-Za-z2,4" 4. NSPredicate
2、60;*emailTest = NSPredicate predicateWithFormat : "SELF MATCHES%" ,emailRegex; 5. return emailTest evaluateWithObject :email; 6. 2.圖片壓縮 1. 用法: UIImage *yourImage
3、= self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0); / 壓縮圖片 - ( UIImage *)imageWithImageSimple:( UIImage *)image scaledToSize:( CGSize )newSize 2. 3. / Create a graphi
4、cs image context UIGraphicsBeginImageContext (newSize); 4. / Tell the old image to draw in this newcontext, with the desired / new size image drawInRect : CGRectMake
5、60;( 0 , 0 ,newSize. width ,newSize. height ); 5. / Get the new image from the context UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); 6. / End
6、 the context UIGraphicsEndImageContext (); 7. / Return the new image. return newImage; 8. 9. 3.親測可用的圖片上傳代碼 1. - ( IBAction )uploadButton:( id )sender &
7、#160;2. UIImage *image = UIImage imageNamed : "1.jpg" / 圖片名 NSData *imageData = UIImageJPEGRepresentation (image, 0.5 );/ 壓縮比例 NSLog ( " 字節(jié)數(shù)
8、60;:%i" ,imageData length); 3. / post url NSString *urlString = "13:8090/text/UploadServlet" 4. / 服務(wù)器地址 / setting up the request object now NSM
9、utableURLRequest *request = NSMutableURLRequest alloc init 5. request setURL : NSURL URLWithString :urlString; 6. request setHTTPMethod : &qu
10、ot;POST" 7. / NSString *boundary = NSString stringWithString : "-14737809831466499882746641449" 8. NSString *contentType = NSString stringWithFormat
11、 : "multipart/form-data;boundary=%" ,boundary; 9. request addValue :contentType forHTTPHeaderField : "Content-Type" 10. / NSMutableData *body = NSMutableData
12、; data 11. body appendData : NSString stringWithFormat : "rn-%rn" ,boundary dataUsingEncoding : NSUTF8StringEncoding 12. body appendData : NSString
13、60; stringWithString : "Content-Disposition:form-data; name="userfile" filename="2.png"rn" dataUsingEncoding : NSUTF8StringEncoding / 上傳上去的圖片名字 body appendData : NSString
14、 stringWithString : "Content-Type: application/octet-streamrnrn" dataUsingEncoding : NSUTF8StringEncoding 13. body appendData : NSData dataWithData :imageData;
15、14. body appendData : NSString stringWithFormat : "rn-%-rn" ,boundary dataUsingEncoding : NSUTF8StringEncoding 15. request setHTTPBody :body; 16. / NSLog(&
16、quot;1-body:%",body); NSLog ( "2-request:%" ,request); 17. NSData *returnData = NSURLConnection sendSynchronousRequest :request returningResponse : nil error :
17、60;nil 18. NSString *returnString = NSString alloc initWithData :returnData encoding : NSUTF8StringEncoding 19. NSLog ( "3- 測試輸出: %" ,returnStri
18、ng ); 4.給imageView加載圖片 1. UIImage *myImage = UIImage imageNamed : "1.jpg" 2. imageView setImage :myImage; 3. self
19、0;. view addSubview : imageView 5.對圖庫的操作 1. 選擇相冊: UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera; 2. if (!UIImagePickerControllerisSourceTypeAvailable:UIImagePickerContro
20、llerSourceTypeCamera) 3. sourceType=UIImagePickerControllerSourceTypePhotoLibrary; 4. 5. UIImagePickerController * picker = UIImagePickerControllerallocinit;
21、; 6. picker.delegate = self; 7. picker.allowsEditing=YES; 8. picker.sourceType=sourceType; 9. self presentModalViewController:picker animated:YES; 10. 選擇完畢:
22、60; -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info 11. 12. picker dismissModalViewControllerAnimated:YES; 13. UIImage * image=info obje
23、ctForKey:UIImagePickerControllerEditedImage; 14. self performSelector:selector(selectPic:) withObject:imageafterDelay:0.1; 15. 16. -(void)selectPic:(UIImage*)image 17. 18. NSLog("image%&
24、quot;,image); 19. imageView = UIImageView alloc initWithImage:image; 20. imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height); 21. self.viewaddSubview:imageView;
25、 22. self performSelectorInBackground:selector(detect:) withObject:nil; 23. 24. detect 為自己定義的方法,編輯選取照片后要實現(xiàn)的效果 取消選擇: -(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker 25. 26.
26、; 27. picker dismissModalViewControllerAnimated:YES; 28. 6.跳到下個View 1. nextWebView = WEBViewController alloc initWithNibName : "WEBViewController" bund
27、le : nil 2. self presentModalViewController : nextWebView animated : YES 7.創(chuàng)建一個UIBarButton右邊按鈕 1. UIBarButtonItem *rightButton = UIBarButtonItem al
28、loc initWithTitle : " 右邊 " style : UIBarButtonItemStyleDone target : self action : selector (clickRightButton); 2. self . navigationItem
29、0; setRightBarButtonItem :rightButton; 8.設(shè)置navigationBar隱藏 1. self . navigationController . navigationBarHidden = YES / 9.UIlabel多行文字自動換行 (自動折行) 1. UIView *footerView = UIView allo
30、cinitWithFrame:CGRectMake(10, 100, 300, 180); UILabel *label = UILabel allocinitWithFrame:CGRectMake(10, 100, 300, 150); label.text = "Hello world! Hello world!Hello world! Hello world! Hell
31、o world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld!" / 背景顏色為紅色 label.backgroundColor = UIColor redColor;
32、0;/ 設(shè)置字體顏色為白色 label.textColor = UIColor whiteColor; / 文字居中顯示 label.textAlignment = UITextAlignmentCenter; / 自動折行設(shè)置 label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; 10.代碼生成Butto
33、n 1. CGRect frame = CGRectMake ( 0 , 400 , 72.0 , 37.0 ); 2. UIButton *button = UIButton buttonWithType : UIButtonTypeRoundedRect
34、; 3. button. frame = frame; 4. button setTitle : " 新添加的按鈕 " forState: UIControlStateNormal 5. button. backgroundColor = UIColor clearColor&
35、#160; 6. button. tag = 2000 7. button addTarget : self action : selector (buttonClicked:) forControlEvents : UIControlEventTouchUpInside 8. self
36、0;. view addSubview :button; 10.2在xib文件中已經(jīng)創(chuàng)建好Button,通過tag獲取按鈕 1. UIButton *testButton= (UIButton*)self.view viewWithTag:100; 2. testButton addTarget:self action:selector(test:) forControlEvents:UICo
37、ntrolEventTouchUpInside; /按鈕事件1. -(void) test: (id) sender 2. UIAlertView *av = UIAlertView alloc initWithTitle:"ceshi" message:"test11111" delegate:nil cancelButtonTitle:"OK"
38、otherButtonTitles:nil autorelease; 3. av show; 4. 11.讓某個控件在View的中心位置顯示:(某個控件,比如 label , View ) label . center = self . view . center; 12.自定義text各種效果: 1. cell.backgroundColor = UIColorscrollViewTexturedBackgroundC
39、olor; 2. / 設(shè)置文字的字體 3. cell.textLabel.font = UIFont fontWithName:"AmericanTypewriter" size:100.0f; 4. / 設(shè)置文字的顏色 5. cell.textLabel.textColor = UIColor orangeColor; 6. / 設(shè)置文字的背景顏色 7. cell.textLab
40、el.shadowColor = UIColor whiteColor; 8. / 設(shè)置文字的顯示位置 9. cell.textLabel.textAlignment = UITextAlignmentCenter; 13.隱藏statusBar: 在程序的 viewDidLoad 中加入 1. UIApplication sharedApplicationsetStatusBarHidden:YES animated:NO; 1
41、4.更改AlertView背景:1. UIAlertView *theAlert = UIAlertViewalloc initWithTitle:"Atention" 2.
42、60; message: "I'm a Chinese!" 3.
43、 delegate:nil 4.
44、60; cancelButtonTitle:"Cancel" 5.
45、 otherButtonTitles:"Okay",nil autoreleas
46、e; 6. theAlert show; 7. UIImage *theImage = UIImageimageNamed:"loveChina.png" 8. theImage = theImage stretchableImageWithLeftCapWidth:0topCapHeight:0;
47、 9. CGSize theSize = theAlert frame.size; 10. UIGraphicsBeginImageContext(theSize); 11. theImage drawInRect:CGRectMake(5, 5, theSize.width-10,
48、theSize.height-20);/ 這個地方的大小要自己調(diào)整,以適應(yīng) alertview 的背景顏色的大小。 12. theImage = UIGraphicsGetImageFromCurrentImageContext(); 13. UIGraphicsEndImageContext(); 14. theAlert.layer.contents =
49、60;(id)theImage CGImage; 15.鍵盤透明: 1. textField.keyboardAppearance = UIKeyboardAppearanceAlert; 16.狀態(tài)欄的網(wǎng)絡(luò)活動風(fēng)火輪是否旋轉(zhuǎn):1. UIApplication sharedAworkActivityIndicatorVisible , 默認值是 NO 。 17.截取屏幕圖片:1. / 創(chuàng)建一個基于位 圖的圖形上下文并指定大小為
50、CGSizeMake(200,400) 2. UIGraphicsBeginImageContext(CGSizeMake(200,400); 3. 4. /renderInContext 呈現(xiàn)接受者及其子范圍到 指定的上下文 5. self.view.layer renderInContext:UIGraphicsGetCurrentContext(); 6. / 返回 一個基于當前圖形上下文的圖片 7.
51、160;UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext(); 8. / 移除棧頂 的基于當前位圖的圖形上下文 9. UIGraphicsEndImageContext(); 10. / 以 png 格式 返回指定圖片的數(shù)據(jù) 11. imageData = UIImagePNGR epresentation(aImage);
52、0;18.更改cell選中的背景: 1. UIView *myview = UIView alloc init; 2. myview.frame = CGRectMake(0, 0, 320, 47); 3. myview.backgroundColor = UIColorcolorWithPatternImage:UIImage imageNamed:"0006.
53、png" 4. cell.selectedBackgroundView = myview;: 19.顯示圖片:1. CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f); 2. UIImageView *myImage = UIImageView alloc initWithFrame:myImageR
54、ect; 3. myImage setImage:UIImage imageNamed:"myImage.png" 4. myImage.opaque = YES; /opaque 是否透明 5. self.view addSubview:myImage; 20.能讓圖片適應(yīng)框的大?。╞eta) 1. NSString*imagePath = NSBundle mainBundle pathF
55、orResource:"XcodeCrash"ofType:"png" 2. UIImage *image = UIImage allocinitWithContentsOfFile:imagePath; 3. UIImage *newImage= image
56、160;transformWidth:80.f height:240.f; 4. UIImageView *imageView = UIImageView allocinitWithImage: newImage; 5. newImagerelease; 6. imag
57、e release; 7. self.view addSubview:imageView; 21. 實現(xiàn)點擊圖片進行跳轉(zhuǎn)的代碼:(生成一個帶有背景圖片的button,給button綁定想要的事件)1. UIButton *imgButton=UIButton allocinitWithFrame:CGRectMake(0, 0, 120, 120); 2. imgButton setBackground
58、Image:(UIImage *)self.imgArray objectAtIndex:indexPath.row forState:UIControlStateNormal; 3. imgButton.tag=indexPath row; 4. imgButton addTarget:self action:selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside; 22.鍵盤回
59、收:1).增加一個button,相應(yīng)touch down事件,隱藏鍵盤。這種方法,太山寨了。為了相應(yīng)一個事件增加一個button太不值得的。1. .h 2. 3. 4. - (IBAction)dismissKeyBoard:(id)sender; 5. 6. .m 7. 8. 9. - (IBAction)dismissKeyBoard:(id)sender 10. 11.
60、160; testText resignFirstResponder; 12. 13. 2).第二種方法:在背景圖片上添加Tap事件,相應(yīng)單擊處理。這種方法,很好代替了button方式,但是如果UI上沒有背景圖片,這種方法又回到到第一種山寨的方法行列中。/ 添加帶有處理時間的背景圖片 1. UIImageView *backView = UIImageView alloc initWithFrame:CGRectMake(0, 0, self.vi
61、ew.bounds.size.width, self.view.bounds.size.height); 2. 3. backView.image = UIImage imageNamed:"small3.png" 4. 5. 6. 7. backView.userInteractionEn
62、abled = YES; 8. 9. UITapGestureRecognizer *singleTouch = UITapGestureRecognizer alloc initWithTarget:self action:selector(dismissKeyboard:); 10. 11. backView addGestureRecog
63、nizer:singleTouch; 12. 13. 14. 15. backView.tag = 110; 16. 17. self.view addSubview:backView; 18. 19. 20. 21. -(void)dismissKeyboard
64、:(id)sender 22. 23. text resignFirstResponder; 24. 25. 3).在xib文件中,修改xib文件的objects屬性,默認是view屬性,我們可以修改為UIControl屬性,從而是xib文件相應(yīng)touch down事件。這種方法,缺點就是沒有xib就悲劇了。1. .h 2. 3. 4. - (IBAction)dimissKeyboard:
65、(id)sender; 5. 6. .m 7. 8. 9. - (IBAction)dimissKeyboard:(id)sender 10. 11. text resignFirstResponder; 12. 13. 23、Gif圖片的解析 1. /加載gif 2. 3. 02
66、; 4. 5. 03 NSString *filePath = NSBundle mainBundlepathForResource:"bai3" ofType:"gif" 6. 7. 04 8. 9. 05 NSData *data
67、 = NSData dataWithContentsOfFile:filePath; 10. 11. 06 12. 13. 07 CGImageSourceRef gif = CGImageSourceCreateWithData(CFDataRef)data, nil); 14. 15. 08 &
68、#160; 16. 17. 09 /獲取gif的各種屬性 18. 19. 10 20. 21. 11 CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL); 22. 23. 12
69、0; 24. 25. 13 NSLog("_%",gifprops); 26. 27. 14 28. 29. 15 30. 31. 16 NSInteger count =CGImageSourceGetCoun
70、t(gif); 32. 33. 17 34. 35. 18 NSLog("_%d",count); 36. 37. 19 38. 39. 20 40. 41. 21 CFDi
71、ctionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary); 42. 43. 22 44. 45. 23 CFDictionaryRef delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);
72、160; 46. 47. 24 48. 49. 25 NSLog("_%",delay); 50. 51. 26 52. 53. 27 54. 55. 28 /gifDic objectF
73、orKey:(NSString *)kCGImagePropertyGIFDelayTime; 56. 57. 29 58. 59. 30 / NSNumber * w = CFDictionaryGetValue(gifprops, "PixelWidth");
74、60. 61. 31 62. 63. 32 / NSNumber * h =CFDictionaryGetValue(gifprops, "PixelHeight"); 64. 65. 33 66. 67. 34 &
75、#160; / float totalDuration = delay.doubleValue * count; 68. 69. 35 70. 71. 36 / float pixelWidth = Value;
76、; 72. 73. 37 74. 75. 38 / float pixelHeight = Value; 76. 77. 39 78. 79. 40 /將gif解析成UIImage類型對象,并加進ima
77、ges數(shù)組中 80. 81. 41 82. 83. 42 84. 85. 43 NSMutableArray *images = NSMutableArray arrayWithCapacity:count; 86. 87. 44
78、160; 88. 89. 45 for(int index=0;index<count;index+) 90. 91. 46 92. 93. 47 94. 95. 48 96. 97. 4
79、9 CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil); 98. 99. 50 100. 101. 51 UIImage *img&
80、#160;= UIImage imageWithCGImage:ref; 102. 103. 52 104. 105. 53 images addObject:img; 106. 107. 54 108. 109. 55
81、 CFRelease(ref); 110. 111. 56 112. 113. 57 114. 115. 58 116. 117. 59 CFRelease
82、(gifprops); 118. 119. 60 120. 121. 61 CFRelease(gif); Gif的合成 1. - (void)exportAnimatedGif:(CGImageSourceRef )gif :(NSMutableArray *)images 2. 3. 02
83、 4. 5. 03 6. 7. 04 8. 9. 05 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject&
84、#160;stringByAppendingPathComponent:"animated.gif" 10. 11. 06 12. 13. 07 CGImageDestinationRef destination = CGImageDestinationCreateWithURL( CFURLRef)NSURL fileURLWithPath:path,
85、 14. 15. 08 16. 17. 09
86、0; kUTTypeGIF, 18. 19. 10 20.
87、60;21. 11
88、60; images.count, 22. 23. 12 24. 25. 13
89、
90、 NULL); 26. 27. 14 28. 29. 15 UIImage *image; 30. 31. 16 32. 33. 17
91、 for (int i = 0; i<images.count; i+) 34. 35. 18 36. 37. 19 38. 39. 20 40. 41. 21
92、 image = imagesi; 42. 43. 22 44. 45. 23 CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,i,NULL); 46
93、. 47. 24 48. 49. 25 CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary); 50. 51. 26 52. 53. 2
94、7 NSNumber *delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime); 54. 55. 28 56. 57. 29 NSDictionary *gifDelay = NSDictionary dictionaryWithObject:NSDictionary dictionaryWithObject:delay forKey:(NSString *)kCGImagePropertyGIFDelayTime &
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度工程物資承包協(xié)議模板
- 2024車輛無息借用協(xié)議模板
- 貸款擔(dān)保專用保證金協(xié)議范本2024
- 城市消防氣體滅火系統(tǒng)工程承攬協(xié)議
- 2024年全職勞務(wù)派遣協(xié)議細則
- 2024汽車租賃替代銷售協(xié)議范本
- 花池維修合同范本
- 制藥企業(yè)合同范本
- 個人租房更改裝修合同范本
- 2024年環(huán)保垃圾收集運輸服務(wù)協(xié)議
- 第一章第三節(jié)《氧化還原反應(yīng)》第一課時高一上學(xué)期化學(xué)人教版(2019)必修第一冊
- 高三政治月考試卷講評
- 藍色簡約風(fēng)中國空軍成立75周年紀念日
- 2024年全國企業(yè)員工全面質(zhì)量管理知識競賽題庫(含答案)(共132題)
- 知識創(chuàng)業(yè)思維與方法智慧樹知到答案2024年湖南師范大學(xué)
- 無人機全行業(yè)保險
- 2023年廣東省建筑設(shè)計研究院校園招聘筆試參考題庫附帶答案詳解
- 員工人事檔案目錄
- 迅達SWE30-100K自動扶梯電路分析_圖文
- 華東理工大學(xué)PPT模板
- 一年級上冊語文期中考試試卷分析
評論
0/150
提交評論