於UIImage圖片處理詳細使用

關於UIImage圖片處理詳細使用

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

轉載web

標籤: 

it

分類: iOS整理

1、使用文件建立(靜態方法)網絡

    UIImage *myImage = [UIImage imageNamed:@"ppp"];app

2、使用 URL 和原始數據(靜態方法)atom

    NSData *imageData = [ NSData initWithBytes:imagePtr length:imageSize ]; 假設 imagePtr 是一個指向原始數據的指針url

    UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];spa


    UIImage *myImage2 =[UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];指針

3、使用Core Graphics (靜態方法)orm

    UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];htm

4、使用文件(實例方法)

    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];

相關文章
相關標籤/搜索