一、使用文件创建(静态方法)
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"]]];
三、使用Core Graphics (静态方法)
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;
[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;
等比率缩放
- (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;
}自定长宽
- (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;
}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這個檔名存到app home底下的Documents目錄裡
//2)储存到手机的图片库里
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data = [NSDatadataWithContentsOfFile:path];
UIImage *img = [UIImageimageWithData:data];