创建BitMap图形上下文的方法:
UIKIT_EXTERN void UIGraphicsBeginImageContext(CGSize size);
UIKIT_EXTERN void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN UIImage* __nullable UIGraphicsGetImageFromCurrentImageContext(void);
UIKIT_EXTERN void UIGraphicsEndImageContext(void);
讲解上面的四个方法:
-
UIGraphicsBeginImageContext
与UIGraphicsBeginImageContextWithOptions
都可以创建基于位图的上下文(context
),并将其设置为当前的上下文。 -
UIGraphicsBeginImageContextWithOptions
参数:
CGSize size
:新创建的位图上下文的大小。
BOOL opaque
: 透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
CGFloat scale
:缩放因子,0代表不缩放(实际上系统会自动设置正确的比例)。
UIGraphicsBeginImageContext
的功能与UIGraphicsBeginImageContextWithOptions
的功能相同,相当于UIGraphicsBeginImageContextWithOptions
的opaque
为NO
,scale
为1
。 -
UIGraphicsGetImageFromCurrentImageContext
将位图上下文生成新的图片。 -
UIGraphicsEndImageContext
关闭位图上下文。
图片的压缩:
压: 指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降。
缩: 指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小。
网友评论