每天學(xué)習(xí)一點(diǎn)點(diǎn)受扳,進(jìn)步一點(diǎn)點(diǎn)械念。
1.runtime為一個(gè)類動(dòng)態(tài)添加屬性
// 動(dòng)態(tài)添加屬性的本質(zhì)是: 讓對象的某個(gè)屬性與值產(chǎn)生關(guān)聯(lián)objc_setAssociatedObject(self, WZBPlaceholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
2、獲取runtime為一個(gè)類動(dòng)態(tài)添加的屬性
objc_getAssociatedObject(self, WZBPlaceholderViewKey);
3.KVO監(jiān)聽某個(gè)對象的屬性
// 添加監(jiān)聽者
[self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil];
// 當(dāng)監(jiān)聽的屬性值變化的時(shí)候會來到這個(gè)方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"property"]) {
[self textViewTextChange];
} else {
}
}
4.Reachability判斷網(wǎng)絡(luò)狀態(tài)
NetworkStatus status = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
if (status == NotReachable) {
NSLog(@"當(dāng)前設(shè)備無網(wǎng)絡(luò)");
}
if (status == ReachableViaWiFi) {
NSLog(@"當(dāng)前wifi網(wǎng)絡(luò)");
}
if (status == ReachableViaWWAN) {
NSLog(@"當(dāng)前蜂窩移動(dòng)網(wǎng)絡(luò)");
}
5.AFNetworking監(jiān)聽網(wǎng)絡(luò)狀態(tài)
// 監(jiān)聽網(wǎng)絡(luò)狀況
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable: {
[SVProgressHUD showInfoWithStatus:@"當(dāng)前設(shè)備無網(wǎng)絡(luò)"];
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[SVProgressHUD showInfoWithStatus:@"當(dāng)前Wi-Fi網(wǎng)絡(luò)"];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
[SVProgressHUD showInfoWithStatus:@"當(dāng)前蜂窩移動(dòng)網(wǎng)絡(luò)"];
break;
default:
break;
}
}];
[mgr startMonitoring];
6.透明顏色不影響子視圖透明度
[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
7.取圖片某一點(diǎn)的顏色
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.根據(jù)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屏幕且存在對應(yīng)圖片座享,則返回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;
}