iOS修改图片尺寸和裁剪功能以及 图片上加图片 图片加文字(水印效果)

1.修改图片尺寸

- (  UIImage  *)imageWithImageSimple:(  UIImage  *)image scaledToSize:(  CGSize  )newSize

{

// Create a graphics image context

UIGraphicsBeginImageContext  (newSize);

// Tell the old image to draw in this new context, with the desired

// new size

[image  drawInRect  :  CGRectMake  (  0  ,  0  ,newSize.  width  ,newSize.  height  )];

// Get the new image from the context

UIImage  * newImage =  UIGraphicsGetImageFromCurrentImageContext  ();

// End the context

UIGraphicsEndImageContext  ();

// Return the new image.

return  newImage;

}

2.裁剪图片

a.

- (  UIImage  *)getImageByCuttingImage:(  UIImage  *)image Rect:(  CGRect  )rect{

//  大图  bigImage

//  定义  myImageRect  ,截图的区域

CGRect  myImageRect = rect;

UIImage  * bigImage= image;

CGImageRef  imageRef = bigImage.  CGImage  ;

CGImageRef  subImageRef =  CGImageCreateWithImageInRect  (imageRef, myImageRect);

CGSize  size;

size.  width  = rect.  size  .  width  ;

size.  height  = rect.  size  .  height  ;

UIGraphicsBeginImageContext  (size);

CGContextRef  context =  UIGraphicsGetCurrentContext  ();

CGContextDrawImage  (context, myImageRect, subImageRef);

UIImage  * smallImage = [  UIImage  imageWithCGImage  :subImageRef];

UIGraphicsEndImageContext  ();

return  smallImage;

}

b.

- (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{

    

    //把像 rect 转化为 rect(如无转化则按原图像素取部分图片)

    CGFloat scale = [UIScreen mainScreen].scale;

    CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;

    CGRect dianRect = CGRectMake(x, y, w, h);

    

    //截取部分图片并生成新图片

    CGImageRef sourceImageRef = [image CGImage];

    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);

    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

    return newImage;

}

3.改变图片大小缩放方法

-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size {

    UIGraphicsBeginImageContext(size); //sizeCGSize类型,即你所需要的图片尺寸

    

    [image drawInRect:CGRectMake(0,0, size.width, size.height)];

    

    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return scaledImage;  //返回的就是已经改变的图片

}

4.图片上加图片

-(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo {

    //get image width and height

    int w = img.size.width;

    int h = img.size.height;

    int logoWidth = logo.size.width/2;

    int logoHeight = logo.size.height/2;

    CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

    //create a graphic context with CGBitmapContextCreate

    CGContextRef context =CGBitmapContextCreate(NULL, w, h,8, 4 * w, colorSpace,kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context,CGRectMake(0,0, w, h), img.CGImage);//先画出第一张图

    CGContextDrawImage(context,CGRectMake(0, h - logoHeight, logoWidth, logoHeight), [logoCGImage]); //再画第二张图

    CGImageRef imageMasked =CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImageimageWithCGImage:imageMasked];

}

5.图片加文字

- (UIImage *)shuiYinOne:(UIImage *)oldImag{

    //开启上下文

    //size新图片的大小

    //opaque YES不透明 NO透明

    UIGraphicsBeginImageContextWithOptions(oldImag.size,NO, 0.0);

    [oldImag drawAtPoint:CGPointZero];

    NSString *str =self.bubbleView.textView.text;

    NSDictionary *dict =@{

                           NSFontAttributeName :self.bubbleView.textView.font,

                           NSForegroundColorAttributeName :Color(158,194, 24)

                           };

    [str drawAtPoint:self.bubbleView.centerwithAttributes:dict];

    

    //获取新的图片

    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

    

    //关闭上下文

    UIGraphicsEndImageContext();

    

    return newImage;

}





你可能感兴趣的:(iOS修改图片尺寸和裁剪功能以及 图片上加图片 图片加文字(水印效果))