创建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{
- CGRect myRect;
- myRect.origin.x = 0.0 ;
- myRect.origin.y = 0.0;
- myRect.size = myImage.size;
- [myImage drawInRect:myRect];
- }
在整个视图区域重复绘制该图像:
- UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
- [myImage drawInRect:myView.frame];
- [self.view addSubview:myView];
只有在视图初次绘制时,才会调用drawRect方法。要强制更新,可以使用视图类的 setNeedsDisplay 或者 setNeedsDisplayInRect 方法:
- [myView setNeedsDisplay];
- [myView setNeedsDisplayInRect:self.view];
图像方向属性:
一个图像的方向,决定了它在屏幕上如何被旋转。因为iPhone 能被以6种不同的方式握持,所以在方向改变时,能够将图像做相应的旋转就十分必要了。UIImage 有个只读属性 imageOrientation 来标识它的方向。
- UIImageOrientation myOrientation = myImage.imageOrientation ;
- typedef enum {
- UIImageOrientationUp, // default orientation 默认方向
- UIImageOrientationDown, // 180 deg rotation 旋转180度
- UIImageOrientationLeft, // 90 deg CCW 逆时针旋转90度
- UIImageOrientationRight, // 90 deg CW 顺时针旋转90度
- UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻转
- UIImageOrientationDownMirrored, // horizontal flip 向下水平翻转
- UIImageOrientationLeftMirrored, // vertical flip 逆时针旋转90度,垂直翻转
- UIImageOrientationRightMirrored, // vertical flip 顺时针旋转90度,垂直翻转
- } UIImageOrientation;
下面是个解决应用图片旋转或颠倒的bug:
- + (UIImage *)fixOrientation:(UIImage *)aImage {
- // No-op if the orientation is already correct
- if (aImage.imageOrientation == UIImageOrientationUp)
- return aImage;
- // We need to calculate the proper transformation to make the image upright.
- // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
- CGAffineTransform transform = CGAffineTransformIdentity;
- switch (aImage.imageOrientation) {
- case UIImageOrientationDown:
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
- transform = CGAffineTransformRotate(transform, M_PI);
- break;
- case UIImageOrientationLeft:
- case UIImageOrientationLeftMirrored:
- transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
- transform = CGAffineTransformRotate(transform, M_PI_2);
- break;
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
- transform = CGAffineTransformRotate(transform, -M_PI_2);
- break;
- default:
- break;
- }
- switch (aImage.imageOrientation) {
- case UIImageOrientationUpMirrored:
- case UIImageOrientationDownMirrored:
- transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRightMirrored:
- transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
- transform = CGAffineTransformScale(transform, -1, 1);
- break;
- default:
- break;
- }
- // Now we draw the underlying CGImage into a new context, applying the transform
- // calculated above.
- CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
- CGImageGetBitsPerComponent(aImage.CGImage), 0,
- CGImageGetColorSpace(aImage.CGImage),
- CGImageGetBitmapInfo(aImage.CGImage));
- CGContextConcatCTM(ctx, transform);
- switch (aImage.imageOrientation) {
- case UIImageOrientationLeft:
- case UIImageOrientationLeftMirrored:
- case UIImageOrientationRight:
- case UIImageOrientationRightMirrored:
- // Grr...
- CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
- break;
- default:
- CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
- break;
- }
- // And now we just create a new UIImage from the drawing context
- CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
- UIImage *img = [UIImage imageWithCGImage:cgimg];
- CGContextRelease(ctx);
- CGImageRelease(cgimg);
- return img;
- }
图片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.图片合成
{
UIGraphicsBeginImageCont
[oneImg drawInRect:CGRectMake(0, 0, oneImg.size.width, oneImg.size.height)];
[twoImg drawInRect:CGRectMake(0,
UIImage *resultImg = UIGraphicsGetImageFromCu
UIGraphicsEndImageContex
return resultImg;
}
對於特定UIView的截屏,可以把當前View的layer,輸出到一個ImageContext中,然後利用這個ImageContext得到UIImage
- -(UIImage*)captureView: (UIView *)theView
- {
- CGRect rect = theView.frame;
- UIGraphicsBeginImageCont
ext(rect.size);
- CGContextRef
context =UIGraphicsGetCurrentCont ext();
- [theView.layer renderInContext:context];
- UIImage *img = UIGraphicsGetImageFromCu
rrentImageContext();
- UIGraphicsEndImageContex
t();
- return img;
- }
}