1. 管理代码块
使用
<#name#>
表示变量
代码块的路径
~/Library/Developer/Xcode/UserData/CodeSnippets
可上传到git以保持多终端同步
2. 获取对象的class
NSLog(@"%s", object_getClassName(object));
3. 怎样快速打开storyboard中某个视图对应的控制器?
打开对应的Assistant
,快捷键组合Control(或 Ctrl)⌃
+Option(或 Alt)⌥
+Command(或 Cmd)⌘
+回车
4. 更安全的string —— NS_EXTENSIBLE_STRING_ENUM
查看SDWebImage
源码的时候无意中看到这么一个定义
使用方法
// .h
typedef NSString * MyStringEnum NS_EXTENSIBLE_STRING_ENUM;
FOUNDATION_EXPORT MyStringEnum const MyStringEnumName; // 个人理解FOUNDATION_EXPORT与extern功能相同,都是声明外部引用【后续补充】
// .m
MyStringEnum const MyStringEnumName = @"name";
// 使用场景1
-(void)testMethod:(MyStringEnum)foo {
NSLog(@"%@", foo);
}
[someClass testMethod:MyStringEnumName];
// 使用场景2
typedef NSDictionary< MyStringEnum, id> MyDictionary;
5. 使用imageWithCIImage
获取到的添加滤镜后的图片时有明显的卡顿,并且这样的图片无法被正确的保存到相册
stackoverflow上的回答 https://stackoverflow.com/a/15886422/8053832
(1) Aspect Fit does stretch the image - to fit. If you don't want the image stretched at all, use Center (for example).
(2)
imageWithCIImage
gives you a very weird beast, a UIImage not based on CGImage, and so not susceptible to the normal rules of layer display. It is really nothing but a thin wrapper around CIImage, which is not what you want. You must convert (render) the CIFilter output thru CGImage to UIImage, thus giving you a UIImage that actually has some bits (CGImage, a bitmap). My discussion here gives you code that demonstrates: http://www.apeth.com/iOSBook/ch15.html#_cifilter_and_ciimage
In other words, at some point you must call CIContext
createCGImage:fromRect:
to generate a CGImageRef from the output of your CIFilter, and pass that on into a UIImage. Until you do that, you don't have the output of your filter operations as a real UIImage.
Alternatively, you can draw the image from
imageWithCIImage
into a graphics context. For example, you can draw it into an image graphics context and then use that image.
What you can't do is display the image from
imageWithCIImage
directly. That's because it isn't an image! It has no underlying bitmap (CGImage). There's no there there. All it is is a set of CIFilter instructions for deriving the image.
解决方案:outputImage(CIImage) -> CGImage -> UIImage
CIFilter * filter = [CIFilter filterWithName:filterName];
CIImage * beginImage = [CIImage imageWithCGImage:self.CGImage];
[filter setValue:beginImage forKey:kCIInputImageKey];
CIImage * outputImage = filter.outputImage;
struct CGImage * outputCGImage = [[CIContext contextWithOptions:nil] createCGImage:outputImage fromRect:outputImage.extent];
UIImage * newImage = [UIImage imageWithCGImage:outputCGImage];
6. dispatch_group
的使用
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("label_name", DISPATCH_QUEUE_CONCURRENT);
for (NSObject * item in items) {
dispatch_group_async(group, queue, ^{
// do something.
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// group task is finished.
});
7. 关于风景照片的一个issue
前置知识点:手机拍照有两个模式:portrait(竖屏/肖像)、landscape(横屏/风景)
landscape的image如果直接在image view上展示则会以拍摄时手机方向为基准正常显示,但是若经过类似tip5等使用CGImage处理后,再进行加载则会变成以竖屏为基准的图片,从而使图片发生横竖屏切换似的旋转。
解决方案 https://stackoverflow.com/a/24731742/8053832 即:
UIImage * newImage = [UIImage imageWithCGImage:someCGImage scale: originImage.scale orientation:originImage.imageOrientation];
8. 两个运算符的彩虹屁写法
- 异或:
场景 - 点击一个按钮时切换其高亮状态
someButton.highlighted ^= YES;
- 条件运算符:
场景 - 如果数组存在则返回该数组不存在则创建一个新的数组
NSArray * someArray = array ? : [NSArray array]; // 其实是array ? array : [NSArray array] 的省略写法
9. NS_OPTIONS
枚举使用
先来看一个SwipeGestureRecognizer手势方向的枚举定义
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
从UISwipeGestureRecognizerDirectionRight = 1 << 0
开始理解,1 << 0
即2的0次方,二进制表示0001
;UISwipeGestureRecognizerDirectionLeft = 1 << 1
为2的1次方0010
,依次类推…
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0, // 0001
UISwipeGestureRecognizerDirectionLeft = 1 << 1, // 0010
UISwipeGestureRecognizerDirectionUp = 1 << 2, // 0100
UISwipeGestureRecognizerDirectionDown = 1 << 3 // 1000
};
这样我们通过UISwipeGestureRecognizerDirection
枚举可以获得上下左右组合的全部方向;
例如:
// UISwipeGestureRecognizerDirectionRight和UISwipeGestureRecognizerDirectionLeft组合方向
UISwipeGestureRecognizerDirection right_left = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft; // 0001 | 0010 = 0011 十进制 3
其他运算:
// 判断上述right_left中是否包含UISwipeGestureRecognizerDirectionDown方向
BOOL isContainDown = right_left & UISwipeGestureRecognizerDirectionDown; // 0011 & 1000 = 0000
// 为上述right_left增加UISwipeGestureRecognizerDirectionUp方向
right_left = right_left | UISwipeGestureRecognizerDirectionUp; // 0011 | 0100 = 0111 十进制 7
// 为上述right_left移除UISwipeGestureRecognizerDirectionUp方向
right_left = right_left & (~UISwipeGestureRecognizerDirectionUp); // 0011 & 1011 = 0011 十进制 3
10. 图片转为UIColor
UIColor * imageColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"niu.jpg"]];
11. Masonry等间距布局
NSArray * buttons = @[cameraButton, saveButton, folderButton, shareButton, redoButton];
[buttons mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:30 leadSpacing:40 tailSpacing:40];
[buttons mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self);
make.height.equalTo(@30);
}];
12. UIActivityViewController分享时不显示文件的名称以及大小或微信等应用不显示,可以传入其path来解决
NSString * filePath = [self.currentPath stringByAppendingPathComponent:fileName];
NSData * fileData = [[NSFileManager defaultManager] contentsAtPath:filePath];
NSURL * fileUrl = [NSURL fileURLWithPath:filePath];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[fileUrl, fileData] applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
NSLog(@"%@", activityError.localizedDescription);
};
13. GL边render边保存视频时(共享pixel buffer)可能存在保存的视频有原始帧的情况
原因:可能是视频帧率太高 在gl处理的过程中获取了处理过程中的pixel buffer
解决方案1:
process后手动调用glFinish()
同步C/GPU资源
解决方案2:
锁buffer
CVPixelBufferLockBaseAddress(_outputPixelBuffer, 0);
… process …
CVPixelBufferUnlockBaseAddress(_outputPixelBuffer, 0);
14. NDC转window coordinate
使用投影矩阵
GLKMatrix4 projection = GLKMatrix4MakeOrtho(0, self.view.frame.size.width, self.view.frame.size.height, 0, 1.0, 100);
GLuint projectionLocation = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projection.m);
15. git切换到某次commit做了修改修改后又新建一个commit后找不到了
~ git reflog # 查看所有操作 找到对应commit对应的id(如:5db6e6ed)
~ git checkout 5db6e6ed
16. bitmap转image (单y通道gray)
int f_width = detectResult.p_segments->p_figure->p_segment->width;
int f_height = detectResult.p_segments->p_figure->p_segment->height;
CGColorSpaceRef f_colorSpace = CGColorSpaceCreateDeviceGray();
int f_iBytesPerPixel = 1;
int f_iBitsPerRow = f_iBytesPerPixel * f_width;
int f_iBitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(detectResult.p_segments->p_figure->p_segment->data,
f_width,
f_height,
f_iBitsPerComponent,
f_iBitsPerRow,
f_colorSpace,
kCGImageAlphaNone | kCGBitmapByteOrderDefault
);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(f_colorSpace);