不多说了,直接贴代码吧
- (void)viewDidLoad {
[superviewDidLoad];
UIImage*image = [UIImageimageNamed:@"portrait01.png"];
CGFloatwidth = image.size.width;
CGFloatheight = image.size.height;
//加图片水印
UIImage*image01 = [selfaddToImage:imageimage:imagewithRect:CGRectMake(0,20,30,30)];
UIImageView*imag = [[UIImageViewalloc]initWithImage:image01];
imag.frame=CGRectMake(10,100, width,height);
[self.viewaddSubview:imag];
//剪切图片
UIImage*image1 =[selfcutImage:imagewithRect:CGRectMake(10,20,60,100)];//
intw = image1.size.width;
inth = image1.size.height;
UIImage*image11 = [selfaddText:image1text:@"剪切"withRect:CGRectMake(10,(h-30)/2, w,30) ];
UIImageView*imag1 = [[UIImageViewalloc]initWithImage:image11];
imag1.frame=CGRectMake(10,210, image1.size.width,image1.size.height);
[self.viewaddSubview:imag1];
//缩小图片
UIImage*image2 = [selfscaleToSize:imagesize:CGSizeMake(image1.size.width, image1.size.height)];
UIImage*image22 = [selfaddText:image2text:@"压缩"withRect:CGRectMake(10,(h-30)/2, w,30) ];
UIImageView*imag2 = [[UIImageViewalloc]initWithImage:image22];
imag2.frame=CGRectMake(10,300, image2.size.width,image2.size.height);
[self.viewaddSubview:imag2];
//压缩图片大小并保存
[selfzipImageData:image];
}
//压缩图片
- (UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size{
//设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
//绘制改变大小的图片
[imgdrawInRect:CGRectMake(0,0, size.width, size.height)];
//从当前context中创建一个改变大小后的图片
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
//使当前的context出堆栈
UIGraphicsEndImageContext();
//返回新的改变大小后的图片
returnscaledImage;
}
//截图图片
- (UIImage*)cutImage:(UIImage*)image withRect:(CGRect)rect
{
CGImageRefimageRef =CGImageCreateWithImageInRect([imageCGImage], rect);
UIImage* img = [UIImageimageWithCGImage:imageRef];
CGImageRelease(imageRef);
returnimg;
}
//压缩图片大小
- (void)zipImageData:(UIImage*)image
{
NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyyMMddHHSSS"];
NSString*currentDateStr = [dateFormatterstringFromDate:[NSDatedate]];
NSString*dateStr = [NSStringstringWithFormat:@"%@.jpg",currentDateStr];
NSString*path = [NSTemporaryDirectory()stringByAppendingPathComponent:dateStr];
if([[NSFileManagerdefaultManager]fileExistsAtPath:path]) {
NSError*error;
[[NSFileManagerdefaultManager]removeItemAtPath:patherror:&error];
}
NSData*imgData =UIImageJPEGRepresentation(image,1);
if([imgDatawriteToFile:pathatomically:YES])
{
NSLog(@"saveSuccess");
}
}
//加文字水印
- (UIImage*) addText:(UIImage*)img text:(NSString*)mark withRect:(CGRect)rect
{
intw = img.size.width;
inth = img.size.height;
UIGraphicsBeginImageContext(img.size);
[[UIColorredColor]set];
[imgdrawInRect:CGRectMake(0,0, w, h)];
if([[[UIDevicecurrentDevice]systemName]floatValue] >=7.0)
{
NSDictionary* dic = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:20.0f],NSFontAttributeName,[UIColorblueColor] ,NSForegroundColorAttributeName,nil];
[markdrawInRect:rectwithAttributes:dic];
}
else
{
//该方法在7.0及其以后都废弃了
[markdrawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
}
UIImage*aimg =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnaimg;
}
//加图片水印
- (UIImage*) addToImage:(UIImage*)img image:(UIImage*)newImage withRect:(CGRect)rect
{
intw = img.size.width;
inth = img.size.height;
UIGraphicsBeginImageContext(img.size);
[imgdrawInRect:CGRectMake(0,0, w, h)];
[newImagedrawInRect:rect];
UIImage*aimg =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnaimg;
}