每天学习一点点,进步一点点。
1.runtime为一个类动态添加属性
// 动态添加属性的本质是: 让对象的某个属性与值产生关联objc_setAssociatedObject(self, WZBPlaceholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
2、获取runtime为一个类动态添加的属性
objc_getAssociatedObject(self, WZBPlaceholderViewKey);
3.KVO监听某个对象的属性
// 添加监听者
[self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil];
// 当监听的属性值变化的时候会来到这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"property"]) {
[self textViewTextChange];
} else {
}
}
4.Reachability判断网络状态
NetworkStatus status = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
if (status == NotReachable) {
NSLog(@"当前设备无网络");
}
if (status == ReachableViaWiFi) {
NSLog(@"当前wifi网络");
}
if (status == ReachableViaWWAN) {
NSLog(@"当前蜂窝移动网络");
}
5.AFNetworking监听网络状态
// 监听网络状况
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable: {
[SVProgressHUD showInfoWithStatus:@"当前设备无网络"];
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[SVProgressHUD showInfoWithStatus:@"当前Wi-Fi网络"];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
[SVProgressHUD showInfoWithStatus:@"当前蜂窝移动网络"];
break;
default:
break;
}
}];
[mgr startMonitoring];
6.透明颜色不影响子视图透明度
[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
7.取图片某一点的颜色
if (point.x < 0 || point.y < 0) return nil;
CGImageRef imageRef = self.CGImage;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
if (point.x >= width || point.y >= height) return nil;
unsigned char *rawData = malloc(height * width * 4);
if (!rawData) return nil;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast
| kCGBitmapByteOrder32Big);
if (!context) {
free(rawData);
return nil;
}
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
int byteIndex = (bytesPerRow * point.y) + point.x * bytesPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
UIColor *result = nil;
result = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
free(rawData);
return result;
8.判断该图片是否有透明度通道
- (BOOL)hasAlphaChannel
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(self.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
9.获得灰度图
+ (UIImage*)covertToGrayImageFromImage:(UIImage*)sourceImage
{
int width = sourceImage.size.width;
int height = sourceImage.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate (nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
if (context == NULL) {
return nil;
}
CGContextDrawImage(context,CGRectMake(0, 0, width, height), sourceImage.CGImage);
CGImageRef contextRef = CGBitmapContextCreateImage(context);
UIImage *grayImage = [UIImage imageWithCGImage:contextRef];
CGContextRelease(context);
CGImageRelease(contextRef);
return grayImage;
}
10.根据bundle中的文件名读取图片
+ (UIImage *)imageWithFileName:(NSString *)name {
NSString *extension = @"png";
NSArray *components = [name componentsSeparatedByString:@"."];
if ([components count] >= 2) {
NSUInteger lastIndex = components.count - 1;
extension = [components objectAtIndex:lastIndex];
name = [name substringToIndex:(name.length-(extension.length+1))];
}
// 如果为Retina屏幕且存在对应图片,则返回Retina图片,否则查找普通图片
if ([UIScreen mainScreen].scale == 2.0) {
name = [name stringByAppendingString:@"@2x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
if ([UIScreen mainScreen].scale == 3.0) {
name = [name stringByAppendingString:@"@3x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path) {
return [UIImage imageWithContentsOfFile:path];
}
return nil;
}