一些小技巧

本文汇总了iOS开发中不常用但实用的技巧,包括获取View在Window的位置、UIImageView的圆角设置、文件存储路径注意事项、图片拉伸不失真、视频截取缩略图、UITableViewCell样式调整等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录下一些不常用的技巧,以防忘记,复制用。


1、获取当前的View在Window的frame:

    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[_myButton convertRect:_myButton.bounds toView:window];


2、UIImageView 和UILabel 等一些控件,需要加这句才能setCorn

_myLabel.layer.masksToBounds = YES;


3、手机上的沙盒路径要加"Documents",不然存储写入失败!mac上不用!

[_myArray writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]  stringByAppendingPathComponent:@"shopCategory.plist"] atomically:YES];

NSArray *tempAllData = [NSArray arrayWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]  stringByAppendingPathComponent:@"shopCategory.plist"]];

4、图片拉伸不失真,如聊天软件对话气泡

1)

    UIImage *tempImage2 = [UIImage imageNamed:@"sub.png"];
    tempImage2 = [tempImage2 stretchableImageWithLeftCapWidth:tempImage2.size.width/2 topCapHeight:0];

2)

    UIImage *tempImage3 = [UIImage imageNamed:@"sub"];

    CGFloat tempH = tempImage3.size.height/2;
    CGFloat tempW = tempImage3.size.width/2;
    
    UIEdgeInsets tempEdg = UIEdgeInsetsMake(tempH, tempW, tempH, tempW);
    
    tempImage3 = [tempImage3 resizableImageWithCapInsets:tempEdg resizingMode:UIImageResizingModeStretch];

5、视频截取缩略图,其中CMTimeMakeWithSeconds(5,1),调整截图帧数/秒数,一般不用特意去修改,不做参数传入,除非片头一段时间都一样的视频。

#import <AVFoundation/AVFoundation.h>

-(UIImage *)getThumbnailImage:(NSString *)videoURL
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    //控制截取时间
    CMTime time = CMTimeMakeWithSeconds(5, 1);
    
    NSError *error = nil;
    
    CMTime actualTime;
    
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    
    CGImageRelease(image);
    
    return thumb;
}

6、cell下划线左边顶住屏幕左边。

    cell.preservesSuperviewLayoutMargins = NO;
    cell.layoutMargins = UIEdgeInsetsZero;
    cell.separatorInset = UIEdgeInsetsZero;

7、去除xcode8冗余信息,虽然已经记住了。

OS_ACTIVITY_MODE    disable


8、播放音频

1)工程内音频

1-1)、获取音频路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"shakebell" ofType:@"wav"];
    
NSURL *url = [NSURL fileURLWithPath:path];

1-2)、创建音频播放ID

SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundID);

1-3)、Play

AudioServicesPlaySystemSound(soundID);


2)系统音频,参数为1000-1351,具体查表,如1007为“sms-received1”
AudioServicesPlaySystemSound(1007);


9、字体自适应

1)、固定的Frame,自适应Font大小,如数量增减,1和1000。

[label1 setAdjustsFontSizeToFitWidth:YES];


2)、固定的Font,自适应Frame,用于信息类显示

[label2 sizeToFit];

3)、固定的Font,获取自适应Frame值,反过来设置Label的Frame,用于信息类显示。这里的100是等下设置Label的width,也是返回的rect.frame.size.width

CGRect rect = [templabel.text boundingRectWithSize:CGSizeMake(100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:templabel.font} context:nil];


10、AFNetworking 检测网络连接状态
[[AFNetworkReachabilityManager sharedManager]startMonitoring];

[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld",status);
    }];


11、编辑相关

1-1)、弹出键盘可能盖住TextField。监听键盘的通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moveView:) name:UIKeyboardDidChangeFrameNotification object:nil];


1-2)、moveView方法里接受通知,tempTime是键盘动画时间,tempY是键盘当前的y轴位置。(接着要移动评论框或者移动后面的ScrollView都可以)

CGFloat tempTime = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat tempY = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].origin.y


2)dealloc记得移除

[[NSNotificationCenter defaultCenter]removeObserver:self];


3)touchesBegan:withEvent && scrollViewDidScroll -->屏幕电机&&屏幕滑动要取消编辑装填

[self.view endEditing:YES];


12、上传图片(头像)

1-1)、把Image打成NSFD

NSData *imagedata = UIImageJPEGRepresentation(tempImage, 1.0);

1-2)、AFNetworking的POST方法填如下。formData:POST方法里的Block参数,name:跟服务器有关,filename:随意填,mimeType:就image/jpg。

[formData appendPartWithFileData:imagedata name:@"imgFile" fileName:@"idontcare.jpg" mimeType:@"image/jpg"];

13、强制布局

[self.view layoutIfNeeded];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值