iOS學習資料七之屬性狀態(tài)

每天學習一點點,進步一點點。

1.runtime為一個類動態(tài)添加屬性

// 動態(tài)添加屬性的本質(zhì)是: 讓對象的某個屬性與值產(chǎn)生關聯(lián)objc_setAssociatedObject(self, WZBPlaceholderViewKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

2、獲取runtime為一個類動態(tài)添加的屬性

objc_getAssociatedObject(self, WZBPlaceholderViewKey);

3.KVO監(jiān)聽某個對象的屬性

// 添加監(jiān)聽者

[self addObserver:self forKeyPath:property options:NSKeyValueObservingOptionNew context:nil];

// 當監(jiān)聽的屬性值變化的時候會來到這個方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if ([keyPath isEqualToString:@"property"]) {

[self textViewTextChange];

} else {

}

}

4.Reachability判斷網(wǎng)絡狀態(tài)

NetworkStatus status = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];

if (status == NotReachable) {

NSLog(@"當前設備無網(wǎng)絡");

}

if (status == ReachableViaWiFi) {

NSLog(@"當前wifi網(wǎng)絡");

}

if (status == ReachableViaWWAN) {

NSLog(@"當前蜂窩移動網(wǎng)絡");

}

5.AFNetworking監(jiān)聽網(wǎng)絡狀態(tài)

// 監(jiān)聽網(wǎng)絡狀況

AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];

[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

switch (status) {

case AFNetworkReachabilityStatusUnknown:

break;

case AFNetworkReachabilityStatusNotReachable: {

[SVProgressHUD showInfoWithStatus:@"當前設備無網(wǎng)絡"];

}

break;

case AFNetworkReachabilityStatusReachableViaWiFi:

[SVProgressHUD showInfoWithStatus:@"當前Wi-Fi網(wǎng)絡"];

break;

case AFNetworkReachabilityStatusReachableViaWWAN:

[SVProgressHUD showInfoWithStatus:@"當前蜂窩移動網(wǎng)絡"];

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.根據(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屏幕且存在對應圖片,則返回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;

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容