UIGraphicsBeginImageCont
void UIGraphicsBeginImageCont ext(CGSize size);
size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCu
该函数的功能同UIGraphicsBeginImageCont
UIGraphicsBeginImageCont
函数原型为:
void UIGraphicsBeginImageCont extWithOptions(CGSize size, BOOL opaque, CGFloat scale);
size——同UIGraphicsBeginImageCont
opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
scale—–缩放因子
UIImageC处理
1、等比缩放
- UIGraphicsBeginImageCont
ext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize); - [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
- UIImage *scaledImage = UIGraphicsGetImageFromCu
rrentImageContext(); - UIGraphicsEndImageContex
t(); - return scaledImage;
- }
2、自定义大小
- - (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
- UIGraphicsBeginImageCont
ext(CGSizeMake(reSize.width, reSize.height)); - [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
- UIImage *reSizeImage = UIGraphicsGetImageFromCu
rrentImageContext(); - UIGraphicsEndImageContex
t(); - return reSizeImage;
- }
3、处理某个特定的view
- -(UIImage*) captureView:(UIView *)theView {
- CGRect rect = theView.frame;
- UIGraphicsBeginImageCont
ext(rect.size); - CGContextRef context = UIGraphicsGetCurrentCont
ext(); - [theView.layer renderInContext:context];
- UIImage *img = UIGraphicsGetImageFromCu
rrentImageContext(); - UIGraphicsEndImageContex
t(); - return img;
- }
4、存储图片
4.1、存储到app的文件里
把要处理的图片以image.png的名字存储到app home地下的Document目录中
- NSString *path = [[NSHomeDirectory()stringByAppendingPathCom
ponent:@"Documents"]stringByAppendingPathCom ponent:@"image.png"]; - [UIImagePNGRepresentation
(image) writeToFile:pathatomically:YES];
4.2、存储到手机的图片库中
- CGImageRef screen = UIGetScreenImage();
- UIImage* image = [UIImage imageWithCGImage:screen];
- CGImageRelease(screen);
- UIImageWriteToSavedPhoto
sAlbum(image, self, nil, nil);
获取当前app的名称和版本号
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- // app名称
- NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
- // app版本
- NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionStri
ng"]; - // app build版本
- NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];
UILabel根据text自动调整大小
- label.text = @"**********";
- CGRect frame = label.frame;
- frame.size.height = 10000; // 设置一个很大的高度
- label.frame = frame;
- [label sizeToFit];
- frame.size.height = label.frame.size.height;
- label.frame = frame;
直接拨打有分机号的电话
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://01011112222,3333"]];
一些有关图像处理的代码片段
- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size; //图片缩放裁剪
- (UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height; //改变大小
+ (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2; //合并图片
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect; //裁剪部分图片
+ (void)imageSavedToPhotosAlbum:(UIImage *)image
didFinishSavingWithError
零)重新设置图片的尺寸
- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageCont
[img drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCu
UIGraphicsEndImageContex
return resImage;
}
-)根据给定得图片,从其指定区域截取一张新得图片
-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageIn
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageCont
CGContextRef context = UIGraphicsGetCurrentCont
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContex
return smallImage;
}
二) 合并两张图片
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageCont
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCu
UIGraphicsEndImageContex
return resultingImage;
}