Ios17個常用代碼整理

1.判斷郵箱格式是否正確的代碼
//利用正則表達式驗證
-(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.圖片壓縮
用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
//壓縮圖片
- (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.親測可用的圖片上傳代碼
- (IBAction)uploadButton:(id)sender {
UIImage *image = [UIImage imageNamed:@"1.jpg"]; //圖片名
NSData *imageData = UIImageJPEGRepresentation(image,0.5);//壓縮比例
NSLog(@"字節數:%i",[imageData length]);
// post url
NSString *urlString = @"http://192.168.1.113:8090/text/UploadServlet";
//服務器地址
// 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]]; //上傳上去的圖片名字
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\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-測試輸出:%@",returnString);
4.給imageView加載圖片
UIImage *myImage = [UIImage imageNamed:@"1.jpg"];
[imageView setImage:myImage];
[self.view addSubview:imageView];
5.對圖庫的操作
選擇相冊:
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];
選擇完畢:
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];
[self performSelector:@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 performSelectorInBackground:@selector(detect:) withObject:nil];
}
detect為自己定義的方法,編輯選取照片后要實現的效果
取消選擇:
-(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
6.跳到下個View
nextWebView = [[WEBViewController alloc] initWithNibName:@"WEBViewController" bundle:nil];
[self presentModalViewController:nextWebView animated:YES];
//創建一個UIBarButtonItem右邊按鈕
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右邊" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];
[self.navigationItem setRightBarButtonItem:rightButton];
設置navigationBar隱藏
self.navigationController.navigationBarHidden = YES;//
iOS開發之UIlabel多行文字自動換行 (自動折行)
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!";
//背景顏色為紅色
label.backgroundColor = [UIColor redColor];
//設置字體顏色為白色
label.textColor = [UIColor whiteColor];
//文字居中顯示
label.textAlignment = UITextAlignmentCenter;
//自動折行設置
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
7.代碼生成button
CGRect frame = CGRectMake(0, 400, 72.0, 37.0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"新添加的按鈕" forState: UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
8.讓某個控件在View的中心位置顯示
(某個控件,比如label,View)label.center = self.view.center;
9.好看的文字處理
以tableView中cell的textLabel為例子:
cell.backgroundColor = [UIColorscrollViewTexturedBackgroundColor];
//設置文字的字體
cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:100.0f];
//設置文字的顏色
cell.textLabel.textColor = [UIColor orangeColor];
//設置文字的背景顏色
cell.textLabel.shadowColor = [UIColor whiteColor];
//設置文字的顯示位置
cell.textLabel.textAlignment = UITextAlignmentCenter;
10.隱藏Status Bar
讀者可能知道一個簡易的方法,那就是在程序的viewDidLoad中加入
[[UIApplication sharedApplication]setStatusBarHidden:YES animated:NO];
11.更改AlertView背景
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:0topCapHeight:0];
CGSize theSize = [theAlert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(5, 5, theSize.width-10, theSize.height-20)];//這個地方的大小要自己調整,以適應alertview的背景顏色的大小。
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
theAlert.layer.contents = (id)[theImage CGImage];
12.鍵盤透明
textField.keyboardAppearance = UIKeyboardAppearanceAlert;
狀態欄的網絡活動風火輪是否旋轉
[UIApplication sharedApplication].networkActivityIndicatorVisible,默認值是NO。
13.截取屏幕圖片
//創建一個基于位圖的圖形上下文并指定大小為CGSizeMake(200,400)
UIGraphicsBeginImageContext(CGSizeMake(200,400));
//renderInContext 呈現接受者及其子范圍到指定的上下文
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//返回一個基于當前圖形上下文的圖片
UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
//移除棧頂的基于當前位圖的圖形上下文
UIGraphicsEndImageContext();
//以png格式返回指定圖片的數據
imageData = UIImagePNGRepresentation(aImage);
14.更改cell選中的背景
UIView *myview = [[UIView alloc] init];
myview.frame = CGRectMake(0, 0, 320, 47);
myview.backgroundColor = [UIColorcolorWithPatternImage:[UIImage imageNamed:@"0006.png"]];
cell.selectedBackgroundView = myview;
15.顯示圖像
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; //opaque是否透明
[self.view addSubview:myImage];
16.能讓圖片適應框的大小(沒有確認)
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.實現點擊圖片進行跳轉的代碼:生成一個帶有背景圖片的button,給button綁定想要的事件!
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];

?

轉載于:https://www.cnblogs.com/jiackyan/p/3522386.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273877.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273877.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273877.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ubuntu18 激活 pycharm

1、到官網上下載好對應的版本 2、到安裝好的pycharm的bin文件夾下,找到 pycharm.vmoptions 和 pycharm64.vmoptions,在兩個文件后面添加代碼: -javaagent:-javaagent:/home/maxzhang/user/pycharm/bin/JetbrainsCrack-release-enc.jar&#…

jquery背景自動切換特效

查看效果網址&#xff1a;http://keleyi.com/a/bjad/4kwkql05.htm 本特效的jquery版本只支持1.9.0以下。代碼如下&#xff1a; 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2…

pandas和spark的區別

參考&#xff1a;https://blog.csdn.net/u013613428/article/details/78138857

Android ImageView圖片自適應

網絡上下載下來的圖片自適應&#xff1a;android:adjustViewBounds"true"&#xff08;其詳細解釋在下面&#xff09;<ImageViewandroid:id"id/dynamic_item_image"android:layout_width"wrap_content"android:layout_height"wrap_conten…

Python之IO編程——文件讀寫、StringIO/BytesIO、操作文件和目錄、序列化

BytesIO StringIO操作的只能是str&#xff0c;如果要操作二進制數據&#xff0c;就需要使用BytesIO。BytesIO實現了在內存中讀寫bytes&#xff0c;我們創建一個BytesIO&#xff0c;然后寫入一些bytes&#xff1a; 寫入的不是str&#xff0c;而是經過UTF-8編碼的bytes。 (1).參考…

都江堰很美-佩服古人_Crmhf的一天

地震遺跡&#xff1a;一條背街&#xff0c;損壞嚴重&#xff0c;基本沒什么人。真正的水利工程&#xff0c;值得每個人學習&#xff1a;轉載于:https://www.cnblogs.com/crmhf/p/3823157.html

爬蟲的增量式抓取和數據更新

不管是產生新頁面&#xff0c;還是原本的頁面更新&#xff0c;這種變化都被稱為增量&#xff0c; 而爬取過程則被稱為增量爬取。那如何進行增量式的爬取工作呢&#xff1f;回想一下爬蟲的工作流程&#xff1a; 發送URL請求 ----- 獲得響應 ----- 解析內容 ----- 存儲內容 我們…

Spring Data JPA初使用 *****重要********

Spring Data JPA初使用我們都知道Spring是一個非常優秀的JavaEE整合框架&#xff0c;它盡可能的減少我們開發的工作量和難度。在持久層的業務邏輯方面&#xff0c;Spring開源組織又給我們帶來了同樣優秀的Spring Data JPA。通常我們寫持久層&#xff0c;都是先寫一個接口&#…

flask-筆記

-super() 使用super()保留基模板中定義的原始內容 - link標簽&#xff1a; 用來指定當前文檔和外部資源的關系。它最常見的是用來鏈接樣式表&#xff0c;也用來創建網站圖標(既是網站圖標樣式也包括移動設備和app圖標)。 -csrf: CSRF概念&#xff1a;CSRF跨站點請求偽造(…

MySQL 無法連接

Host localhost is not allowed to connect to this MySQL server 錯誤 解決辦法&#xff1a; C:\Program Files\MySQL\MySQL Server 5.5\my.ini 在[mysqld]下加下面兩行&#xff0c; skip-name-resolve skip-grant-tables 重啟mysql的windows服務&#xff0c;在mysql命令行界面…

能讓你少寫1000行代碼的20個正則表達式

參考: (1).http://www.codeceo.com/article/20-regular-expressions.html

http請求中的Query String Parameters、Form Data、Request Payload

參考: (1).(http請求參數之Query String Parameters、Form Data、Request Payload) - https://www.jianshu.com/p/c81ec1a547ad

蜜罐

http://www.projecthoneypot.org/home.php轉載于:https://www.cnblogs.com/diyunpeng/p/3534507.html

php中json_decode返回數組或對象的實例

1.json_decode() json_decode (PHP 5 > 5.2.0, PECL json > 1.2.0) json_decode — 對 JSON 格式的字符串進行編碼 說明 mixed json_decode ( string $json [, bool $assoc ] ) 接受一個 JSON 格式的字符串并且把它轉換為 PHP 變量 參數 json 待解碼的 json string 格式的…

如何精通js

參考: (1.)https://www.zhihu.com/search?typecontent&q%E5%A6%82%E4%BD%95%E7%B2%BE%E9%80%9Ajs

程序員怎么樣才能進入微軟?

程序員怎么樣才能進入微軟&#xff1f; 程序員到微軟中國總裁 “打工皇帝”長沙曬成功之道 程序員面試之道之走進微軟 應該是西北大學的學生&#xff0c;距離我好近&#xff08;我也在西安&#xff09;&#xff0c;可是又好遠&#xff08;人家拿到了MS的offer&#xff09;。 專…

python中的裝飾器-(重復閱讀)

---1--- 假設我們要增強某個函數的功能&#xff0c;比如&#xff0c;在函數調用前后自動打印日志&#xff0c;但又不希望修改某個函數的定義&#xff0c;這種在代碼運行期間動態增加功能的方式&#xff0c;稱之為“裝飾器”&#xff08;Decorator). 裝飾器本質上是一個Python…

[轉帖]好技術領導,差技術領導

團隊合作一個優秀的技術領導必然是團隊的一份子&#xff0c;他們認為當整個團隊成功時自己才稱得上成功。他們不僅要做好繁雜和不討好的本職工作&#xff0c;還要清除項目中的障礙&#xff0c;從而讓整個團隊能夠以100%的效率運轉起來。一個好的技術領導會努力拓寬團隊在技術上…

python有哪些常用的庫

參考: (1).https://www.zhihu.com/question/20501628/answer/19542741(Python 常用的標準庫以及第三方庫有哪些&#xff1f;)

C#打開文件對話框和文件夾對話框

打開文件對話框OpenFileDialog OpenFileDialog ofd new OpenFileDialog();ofd.Filter "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件|*.*";ofd.ValidateNames true;ofd.CheckPathExists true;ofd.CheckFileExists true;if (ofd.ShowDialog() DialogResult.O…