打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于UIImage图片处理详细使用

 一、使用文件创建(静态方法)

    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(00, 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(00, 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= [UIImageimageWithData: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];

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
iOS 通过URL获取图片,并保存到本地
UIImage 图像-IOS开发
iOS学习——图片压缩到指定大小以内
iOS开发多线程之队列组----下载合并图片
读码笔记-YYWebImage源码 (三) -YYImageCache
详解iOS多图下载的缓存机制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服