不多說了,直接貼代碼吧
- (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;
}