于UIImage图片处理详细使用

关于UIImage图片处理详细使用

 (2013-01-17 11:17:43)

转载

标签: 

it

分类: iOS整理

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

    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 *)getImageFromImage{ 

    //大图bigImage 

    //定义myImageRect,截图的区域 

    CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0); 

    UIImage* bigImage= [UIImage imageNamed:@"k00030.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; 

}

等比率缩放

- (UIImage *)scaleImage:(UIImage *)image toScale:(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 *)image toSize:(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這個檔名存到app home底下的Documents目錄裡

    //2)储存到手机的图片库里

读取本地图片

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];

        NSData *data = [NSDatadataWithContentsOfFile:path];

        UIImage *img = [UIImageimageWithData:data];


你可能感兴趣的:(于UIImage图片处理详细使用)