一、使用文件创建(静态方法)
UIImage *myImage= [UIImage imageNamed:@"ppp"];
二、使用 URL 和原始数据(静态方法)
NSData *imageData= [ NSData initWithBytes:imagePtr length:imageSize]; 假设 imagePtr 是一个指向原始数据的指针
UIImage*myImage = [ [ UIImage alloc ]initWithData:imageData ];
UIImage *myImage2=[UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
三、使用CoreGraphics (静态方法)
UIImage*myImage3 = [UIImage imageWithCGImage:myCGImageRef];
四、使用文件(实例方法)
UIImage
*myImage4 = [[
UIImage
alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];
只要一个视图对象的窗口的某些部分需要绘制,就可以调用它的drawRect方法。要在窗口内部显示一个 UIImage 的内容,可以调用该对象的 drawRect 方法:
- (void)drawRect:(CGRect)rect{
CGRect myRect;
myRect.origin.x = 0.0 ;
myRect.origin.y = 0.0;
myRect.size= myImage.size;
[myImagedrawInRect:myRect];
}
设置图像的方向,UIImage 有个只读属性imageOrientation 来标识它的方向。
UIImageOrientation myOrientation= myImage.imageOrientation;
// typedefenum {
// UIImageOrientationUp, // defaultorientation 默认方向
// UIImageOrientationDown, // 180 degrotation 旋转180度
// UIImageOrientationLeft, // 90 degCCW 逆时针旋转90度
// UIImageOrientationRight, // 90 degCW 顺时针旋转90度
// UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontalflip 向上水平翻转
// UIImageOrientationDownMirrored, // horizontalflip 向下水平翻转
// UIImageOrientationLeftMirrored, // verticalflip 逆时针旋转90度,垂直翻转
// UIImageOrientationRightMirrored, // verticalflip 顺时针旋转90度,垂直翻转
// }UIImageOrientation;
根据给定得图片,从其指定区域截取一张新得图片
-(UIImage*)getImageFromImage{
//大图bigImage
//定义myImageRect,截图的区域
CGRectmyImageRect = CGRectMake(10.0, 10.0, 57.0,57.0);
UIImage*bigImage= [UIImageimageNamed:@"k00030.jpg"];
CGImageRef imageRef =bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef,myImageRect);
CGSizesize;
size.width = 57.0;
size.height = 57.0;
UIGraphicsBeginImageContext(size);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextDrawImage(context, myImageRect,subImageRef);
UIImage*smallImage = [UIImageimageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
returnsmallImage;
}
等比率缩放
-(UIImage *)scaleImage:(UIImage *)imagetoScale:(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;
}
自定长宽
-(UIImage *)reSizeImage:(UIImage *)imagetoSize:(CGSize)reSize
{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
读取网络图片
NSString *urlStr= [result valueForKey:@"profile_image_url"];
NSURL *url= [NSURLURLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imgData= [NSDatadataWithContentsOfURL:url];
UIImage
*img= [
UIImage
imageWithData
:imgData];
储存图片
//1) 储存到app的文件里
UIImage *image;
NSString *path= [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
//這樣就把你要處理的圖片, 以image.png這個檔名存到apphome底下的Documents目錄裡
//2)储存到手机的图片库里
读取本地图片
NSString *path= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data= [NSDatadataWithContentsOfFile:path];
UIImage *img= [UIImageimageWithData:data];