iOS UIimage geekband

這段代碼動態(tài)的創(chuàng)建了一個UIButton,并且把相關(guān)常用的屬性都列舉了
一、使用文件創(chuàng)建(靜態(tài)方法)

UIImage *myImage = [UIImage imageNamed:@"imageName"];

二、使用 URL 和原始數(shù)據(jù)(靜態(tài)方法)
//NSData *imageData = [ NSData initWithBytes:imagePtr length:imageSize ]; //假設(shè) imagePtr 是一個指向原始數(shù)據(jù)的指針
//UIImage *myImage = [ [ UIImage alloc ]initWithData:imageData ];
//[NSURLURLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];
三、使用Core Graphics (靜態(tài)方法)

UIImage* myImage3 = [UIImageimageWithCGImage:myCGImageRef];

四、使用文件(實(shí)例方法)

UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];

只要一個視圖對象的窗口的某些部分需要繪制,就可以調(diào)用它的drawRect方法。要在窗口內(nèi)部顯示一個 UIImage 的內(nèi)容,可以調(diào)用該對象的 drawRect 方法:

-(void)drawRect:(CGRect)rect{
CGRect myRect;
myRect.origin.x = 0.0 ;
myRect.origin.y = 0.0;
myRect.size = myImage.size;
[myImage drawInRect:myRect];
}

設(shè)置圖像的方向,UIImage 有個只讀屬性imageOrientation 來標(biāo)識它的方向。

UIImageOrientation myOrientation = myImage.imageOrientation;
// typedef enum {
// UIImageOrientationUp, // default orientation 默認(rèn)方向
// UIImageOrientationDown, // 180 deg rotation 旋轉(zhuǎn)180度
// UIImageOrientationLeft, // 90 deg CCW 逆時針旋轉(zhuǎn)90度
// UIImageOrientationRight, // 90 deg CW 順時針旋轉(zhuǎn)90度
// UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 向上水平翻轉(zhuǎn)
// UIImageOrientationDownMirrored, // horizontal flip 向下水平翻轉(zhuǎn)
// UIImageOrientationLeftMirrored, // vertical flip 逆時針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// UIImageOrientationRightMirrored, // vertical flip 順時針旋轉(zhuǎn)90度,垂直翻轉(zhuǎn)
// } UIImageOrientation;

根據(jù)給定得圖片,從其指定區(qū)域截取一張新得圖片

-(UIImage )getImageFromImage{
//大圖bigImage
//定義myImageRect,截圖的區(qū)域
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(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;
    }

讀取網(wǎng)絡(luò)圖片

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)儲存到手機(jī)的圖片庫里

讀取本地圖片

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"headimg.png"];
NSData *data = [NSDatadataWithContentsOfFile:path];
UIImage *img = [UIImageimageWithData:data];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容