iOS开发代码片段整理.Part1

1.将NavigationBar设置为透明

viewWillAppear

1
2
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]] forBarMetrics:0];
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]]];

记得在viewWillDisappear中设置回原来的颜色

1
2
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:BlackCustom] forBarMetrics:0];
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:BlackCustom]];

2.创建纯色图片

1
2
3
@interface UIImage (ImageExtend)
+ (UIImage *)imageWithColor:(UIColor *)color;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@implementation  UIImage (ImageExtend)

+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

3.水印图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
加半透明水印
@param useImage 需要加水印的图片
@param addImage1 水印
@returns 加好水印的图片
*/
+ (UIImage *)addImage:(UIImage *)useImage addMaskImage:(UIImage *)maskImage
{
CGFloat w = useImage.size.width;
CGFloat h = useImage.size.height;

//TV提供的useImage的尺寸有所不同,用scale作统一化
CGFloat scale = w /1920.0;
//NSLog(@"scale:%f",scale);

//这个数值不是pt,且为px ; 应按照画布的坐标体系(左下角为原点)
CGFloat logoWidth = maskImage.size.width*scale;
CGFloat logoHeight = maskImage.size.height*scale;
//NSLog(@"useImageWidth:%f,useImageHeight:%f",w,h);
//NSLog(@"logoWidth:%f,logoHeight:%f",logoWidth,logoHeight);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//create a graphic context with CGBitmapContextCreate
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), useImage.CGImage);
CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [maskImage CGImage]);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}

注意:用image.size.width得到的尺寸,在数值上是原图useImage的尺寸,单位为px,所以原图的分辨率直接影响了最后水印的尺寸,所以要做了一下同一化。

4. RGBA与Hex Color

1
2
3
4
5
6
7
8
//#根据十六进制换算成颜色
#define UIColorBaseRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//#根据十进制换算成颜色并可以设置透明度
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f \
alpha:(a)]

5.设备信息

1
2
3
4
5
6
7
8
9
10
11
#define INTERFACE_IS_PAD     ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define INTERFACE_IS_PHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define isPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPadMini ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(768, 1024), [[UIScreen mainScreen] currentMode].size) : NO)

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

#define DEV_UUID [[UIDevice currentDevice].identifierForVendor UUIDString]

#define BUNDLE_VERSION NSDictionary [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"]
那强 wechat
加个微信 成为朋友吧