收集的常用有用代码

收集的常用有用代码,第1张

概述* 给导航条加阴影 if ([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){ [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"head"] forBarMetric

* 给导航条加阴影

if ([self.navigationbar respondsToSelector:@selector(setBackgroundImage:forbarMetrics:)]){        [[UINavigationbar appearance] setBackgroundImage:[UIImage imagenamed:@"head"] forbarMetrics:UIbarMetricsDefault];        self.navigationbar.layer.masksToBounds = NO;        //设置阴影的高度        self.navigationbar.layer.shadowOffset = CGSizeMake(0,3);        //设置透明度        self.navigationbar.layer.shadowOpacity = 0.6;        self.navigationbar.layer.shadowPath = [UIBezIErPath bezIErPathWithRect:self.navigationbar.bounds].CGPath;    }



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,320,47);

myvIEw.backgroundcolor = [UIcolorcolorWithPatternImage:[UIImage imagenamed:@"0006.png"]];

cell.selectedBackgroundVIEw = myvIEw;


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,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=\"user@R_404_6852@\";  @R_404_6852@name=\"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);


iOS开发之UIlabel多行文字自动换行 (自动折行)

UIVIEw *footerVIEw = [[UIVIEw alloc]initWithFrame:CGRectMake(10,100,300,180)];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,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;


3、在程序中播放声音:

首先在程序添加AudioToolBox:

其次,在有播放声音方法的.m方法添加#import:

#import<AudioToolBox/AudioToolBox.h>

接下来,播放声音的代码如下:

Nsstring *path = [[NSBundle mainBundle] pathForResource:@"sound@R_404_6852@name" ofType:@"wav"];

SystemSoundID soundID;

AudioServicesCreateSystemSoundID ((__brIDge CFURLRef)[NSURL @R_404_6852@URLWithPath:path],&soundID);

AudioServicesPlaySystemSound (soundID);


7、捕捉程序关闭或者进入后台事件:

UIApplication *app = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addobserver:self  selector:@selector(applicationWillResignActive:)  name:UIApplicationWillResignActiveNotification object:app];

applicationWillResignActive:这个方法中添加想要的 *** 作

总结

以上是内存溢出为你收集整理的收集的常用有用代码全部内容,希望文章能够帮你解决收集的常用有用代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053716.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存