12.UIImage

创建img及获取图片常用方法

一,网络图片获取显示

UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@ "http://www.huimeeting.com/20080327095245737.png" ]]];

二,本地图片获取显示

UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];  


设置图片投影效果

UIImage *image = [UIImage imageNamed:imageName];

imgView.layer.contents = (id) image.CGImage;//多张图片滑动时,改句解决了卡顿的现象

[[imgView layer] setShadowPath:[UIBezierPath bezierPathWithRect:imgView.bounds].CGPath];

[[imgView layer] setShadowOffset:CGSizeMake(1, 1)];

[[imgView layer] setShadowRadius:5];

[[imgView layer] setShadowOpacity:1];

[[imgView layer] setShadowColor:[UIColor blackColor].CGColor]; //投影个属性设置。

[self.pageScroll addSubview:imgView];


把图片当背景设置
//第一种方法
UIImage *bgImage = [UIImage imageNamed:@"bg.png"];//
 
 
最方便,最快捷的加载图片方式。但是,利用该方式加载图片,在第一次加载之后,图片会cache
在内存中,图片太多时要及时的释放内存。
self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];   
//第二种方法
NSString *path = [[NSBundle mainBundle] pathForResource:@"bg.png" ofType:nil inDirectory:@""];
UIImage *bgImage2 = [[UIImage alloc]initWithContentsOfFile:path];//使用较多,不会将图片 cache 在内存中,但是需要提供图片的具体路径和图片名
self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage2];


要在窗口内 部显示一个 UIImage 的内容,可以调用该对象的 drawRect 方法:

- (void)drawRect:(CGRect)rect{  

  1.     CGRect myRect;  
  2.       
  3.     myRect.origin.x = 0.0 ;  
  4.     myRect.origin.y = 0.0;  
  5.     myRect.size = myImage.size;  
  6.     [myImage drawInRect:myRect];  
  7. }  

在整个视图区域重复绘制该图像:

  1. UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(00200200)];  
  2.     [myImage drawInRect:myView.frame];  
  3.     [self.view addSubview:myView];
注意不要在drawRect方法内分配任何新对象,因为他在每次窗口重绘时都被调用。 

只有在视图初次绘制时,才会调用drawRect方法。要强制更新,可以使用视图类的 setNeedsDisplay 或者 setNeedsDisplayInRect  方法:

  1. [myView setNeedsDisplay];  
  2.     [myView setNeedsDisplayInRect:self.view];  


图像方向属性:

一个图像的方向,决定了它在屏幕上如何被旋转。因为iPhone 能被以6种不同的方式握持,所以在方向改变时,能够将图像做相应的旋转就十分必要了。UIImage 有个只读属性 imageOrientation 来标识它的方向。

  1. UIImageOrientation myOrientation =  myImage.imageOrientation ;  
可以设置以下方向:
  1. typedef enum {  
  2.     UIImageOrientationUp,            // default orientation  默认方向  
  3.     UIImageOrientationDown,          // 180 deg rotation    旋转180度  
  4.     UIImageOrientationLeft,          // 90 deg CCW         逆时针旋转90度  
  5.     UIImageOrientationRight,         // 90 deg CW          顺时针旋转90度  
  6.     UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip   向上水平翻转  
  7.     UIImageOrientationDownMirrored,  // horizontal flip    向下水平翻转  
  8.     UIImageOrientationLeftMirrored,  // vertical flip      逆时针旋转90度,垂直翻转  
  9.     UIImageOrientationRightMirrored, // vertical flip      顺时针旋转90度,垂直翻转  
  10. } UIImageOrientation;  

下面是个解决应用图片旋转或颠倒的bug:

  1. + (UIImage *)fixOrientation:(UIImage *)aImage {  
  2.       
  3.     // No-op if the orientation is already correct  
  4.     if (aImage.imageOrientation == UIImageOrientationUp)   
  5.         return aImage;  
  6.       
  7.     // We need to calculate the proper transformation to make the image upright.  
  8.     // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.  
  9.     CGAffineTransform transform = CGAffineTransformIdentity;  
  10.       
  11.     switch (aImage.imageOrientation) {  
  12.         case UIImageOrientationDown:  
  13.         case UIImageOrientationDownMirrored:  
  14.             transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);  
  15.             transform = CGAffineTransformRotate(transform, M_PI);  
  16.             break;  
  17.               
  18.         case UIImageOrientationLeft:  
  19.         case UIImageOrientationLeftMirrored:  
  20.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
  21.             transform = CGAffineTransformRotate(transform, M_PI_2);  
  22.             break;  
  23.               
  24.         case UIImageOrientationRight:  
  25.         case UIImageOrientationRightMirrored:  
  26.             transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);  
  27.             transform = CGAffineTransformRotate(transform, -M_PI_2);  
  28.             break;  
  29.         default:  
  30.             break;  
  31.     }  
  32.       
  33.     switch (aImage.imageOrientation) {  
  34.         case UIImageOrientationUpMirrored:  
  35.         case UIImageOrientationDownMirrored:  
  36.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
  37.             transform = CGAffineTransformScale(transform, -11);  
  38.             break;  
  39.               
  40.         case UIImageOrientationLeftMirrored:  
  41.         case UIImageOrientationRightMirrored:  
  42.             transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);  
  43.             transform = CGAffineTransformScale(transform, -11);  
  44.             break;  
  45.         default:  
  46.             break;  
  47.     }  
  48.       
  49.     // Now we draw the underlying CGImage into a new context, applying the transform  
  50.     // calculated above.  
  51.     CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,  
  52.                                              CGImageGetBitsPerComponent(aImage.CGImage), 0,  
  53.                                              CGImageGetColorSpace(aImage.CGImage),  
  54.                                              CGImageGetBitmapInfo(aImage.CGImage));  
  55.     CGContextConcatCTM(ctx, transform);  
  56.     switch (aImage.imageOrientation) {  
  57.         case UIImageOrientationLeft:  
  58.         case UIImageOrientationLeftMirrored:  
  59.         case UIImageOrientationRight:  
  60.         case UIImageOrientationRightMirrored:  
  61.             // Grr...  
  62.             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);  
  63.             break;  
  64.               
  65.         default:  
  66.             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);  
  67.             break;  
  68.     }  
  69.       
  70.     // And now we just create a new UIImage from the drawing context  
  71.     CGImageRef cgimg = CGBitmapContextCreateImage(ctx);  
  72.     UIImage *img = [UIImage imageWithCGImage:cgimg];  
  73.     CGContextRelease(ctx);  
  74.     CGImageRelease(cgimg);  
  75.     return img;  
  76. }  





图片img相关处理操作很实用哦

//自定义长宽

+(UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize

{

    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));

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

    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return reSizeImage;

    

}


//等比例缩放

+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize

{

    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));

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

    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;

}


//缩放图片尺寸

-(CGSize)changeImageSize:(UIImage *)img

{

    if (img.size.width == 0 && img.size.height == 0) {

        return CGSizeMake(0, 0);

    }

    CGFloat wscale = EVIsPad()?(img.size.width/frameWidth):(img.size.width / 960);

    CGFloat hscale = EVIsPad()?(img.size.height/frameHeight):(img.size.height / 640);

    CGFloat scale = (wscale > hscale)?wscale:hscale;

    CGSize newSize = CGSizeMake(img.size.width/scale, img.size.height/scale);

    return  newSize;

}



1.图片合成

 - (UIImage *)addTwoImageToOne:(UIImage *) oneImg twoImage:(UIImage *) twoImg

{

UIGraphicsBeginImageContext(oneImg.size);

[oneImg drawInRect:CGRectMake(00, oneImg.size.width, oneImg.size.height)];

[twoImg drawInRect:CGRectMake(00twoImg.size.widthtwoImg.size.height)];

 

UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultImg;

}

對於特定UIView的截屏,可以把當前View的layer,輸出到一個ImageContext中,然後利用這個ImageContext得到UIImage

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

  9. return img;
  10. }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值