ios--图片处理(修改、保存)

来源:互联网 发布:ubuntu如何安装eclipse 编辑:程序博客网 时间:2024/06/06 15:49

UIGraphicsBeginImageContext创建一个基于位图的上下文(context),并将其设置为当前上下文(context)。方法声明如下

void UIGraphicsBeginImageContext(CGSize size);

size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。

该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。

UIGraphicsBeginImageContextWithOptions

函数原型为:

void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);

size——同UIGraphicsBeginImageContext

opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。

scale—–缩放因子


UIImageC处理

1、等比缩放

C代码     - (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
  1. UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
  2. [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  3. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  4. UIGraphicsEndImageContext();
  5. return scaledImage;
  6. }

 

2、自定义大小

C代码 
  1. - (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
  2. UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
  3. [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
  4. UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6. return reSizeImage;
  7. }

 

3、处理某个特定的view

只要是继承UIView的object 都可以处理
必须先import QuzrtzCore.framework

C代码 
  1. -(UIImage*) captureView:(UIView *)theView {
  2. CGRect rect = theView.frame;
  3. UIGraphicsBeginImageContext(rect.size);
  4. CGContextRef context = UIGraphicsGetCurrentContext();
  5. [theView.layer renderInContext:context];
  6. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  7. UIGraphicsEndImageContext();
  8. return img;
  9. }

 

4、存储图片

4.1、存储到app的文件里

把要处理的图片以image.png的名字存储到app home地下的Document目录中

 

C代码 
  1. NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
  2. [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];

4.2、存储手机的图片库中

C代码 
  1. CGImageRef screen = UIGetScreenImage();
  2. UIImage* image = [UIImage imageWithCGImage:screen];
  3. CGImageRelease(screen);
  4. UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

 

 

 

获取当前app的名称和版本号

C代码 
  1. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  2. // app名称
  3. NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
  4. // app版本
  5. NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  6. // app build版本
  7. NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];

 

 

UILabel根据text自动调整大小

 

C代码 
  1. label.text = @"**********";
  2. CGRect frame = label.frame;
  3. frame.size.height = 10000; // 设置一个很大的高度
  4. label.frame = frame;
  5. [label sizeToFit];
  6. frame.size.height = label.frame.size.height;
  7. label.frame = frame;

 

 

直接拨打有分机号的电话

 

C代码 
  1. [[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:(NSError *)error contextInfo:(void *)contextInfo; //保存图片到媒体库


零)重新设置图片的尺寸

- (UIImage *)rescaleImage:(UIImage *)img ToSize:(CGSize)size {

CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);

UIGraphicsBeginImageContext(rect.size);

[img drawInRect:rect]; // scales image to rect

UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

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 = CGImageCreateWithImageInRect(imageRef, myImageRect);

CGSize size;

size.width = 57.0;

size.height = 57.0;

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

return smallImage;

}

二) 合并两张图片

- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {

UIGraphicsBeginImageContext(image1.size);

// 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 = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

0 0