一,1.使用文件创建(静态方法)
UIImage *myImage = [UIImage imageNamed:@"bob"];
2.使用 URL 和原始数据(静态方法)
NSData *imageData = [ NSData initWithBytes:imagePtr length:imageSize ]; 假设 imagePtr 是一个指向原始数据的指针
UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];
UIImage *myImage2 =[UIImage imageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
3.使用Core Graphics (静态方法)
UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];
4.使用文件(实例方法)
UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
5.只要一个视图对象的窗口的某些部分需要绘制,就可以调用它的drawRect方法。要在窗口内部显示一个 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];
}
二,设置图像的方向,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;
1. 根据给定得图片,从其指定区域截取一张新得图片
-(UIImage *)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
UIImage* bigImage= [UIImage imageNamed:@"图片名字.jpg"];
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
CGSize size;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect, subImageRef);
UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}
2. 等比率缩放
- (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;
}
3.自定长宽
- (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;
}
4.读取网络图片
NSString *urlStr = [result valueForKey:@"profile_image_url"];
NSURL *url = [NSURLURLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imgData = [NSDatadataWithContentsOfURL:url];
UIImage *img = [UIImageimageWithData:imgData];
5.储存图片
储存到app的文件里
UIImage *image;
NSString *path =[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
6.读取本地图片
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data = [NSDatadataWithContentsOfFile:path];
UIImage *img = [UIImageimageWithData:data];