UIKit中有一些類可以用來操縱單個圖像,還有一個圖像類可以用來顯示圖像。Apple還提供了一種特殊的導航控制器,用於從圖像庫中選擇圖像。 UIImage類對圖像及其底層數據進行封裝。它可以直接繪制在一個視圖內,或者作為一個圖像容器在另一個更大的圖像視圖容器中使用。這個類類提供的方法可以用來從各種來源中載入圖像,在屏幕上設置圖片的方向,以及提供有關圖像的信息。對於簡單的圖形應用,可以將UIImage對象用在視圖類的drawRect方法中,用來繪制圖像和團模板。 你可以用文件來初始化,也可以用url、原始數據、或者一個Core Graphics圖像的內容。靜態方法(類方法)和實例方法都有;這些方法可以引用並緩存已有的圖像內容,也可以實例化新的圖像對象,如何使用完全取決於應用程序的需要。 使用一個圖像的最簡單方法就是通過靜態方法。靜態方法不會去管理圖像的實例,與之相反,他們提供了直接的接口,可以用來共享位於框架內部的記憶體緩存對象。這有助於保持應用程序的整潔,也會生去做清理工作的需要。靜態方法和實例方法都可以用來創建相同的對象。 一、使用文件創建(靜態方法) UIImage *myImage = [UIImage imageNamed:@"ppp"]; 二、使用 URL 和原始數據(靜態方法) NSData *imageData = [ NSData initWithBytes:image:imagePtr length:imageSize ]; // 假設 imagePtr 是一個指向原始數據的指針 UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ]; UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"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()]]; 五、使用 URL 和原始數據(實例方法) 如果圖像存儲在內存中,你可以創建一個NSData 對象作為initWithData 方法的原始輸入,來初始化一個UIImage對象。 如果圖像是一張網絡圖片,可以使用NSData來進行預載,然后用它來初始化UIImage對象: UIImage *myImage5 =[ [ UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]] ]; 六、使用Core Graphics (實例方法) UIImage* myImage6 = [[UIImage alloc]initWithCGImage:myCGImageRef]; 七、顯示圖像 當視圖類的drawRect 方法被喚起時,它們會調用內部的回吐例程。與其他圖像類不同,UIImage對象不能被當成子 ,直接附着在其他視圖上,因為他不是一個視圖類。反過來,一個UIView類則可以在視圖的drawRect例程中,調用圖像的 drawRect 方法。這可以使得圖像顯在UIView類的顯示區域內部。 只要一個視圖對象的窗口的某些部分需要繪制,就可以調用它的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]; } 注意不要在drawRect方法內分配任何新對象,因為他在每次窗口重繪時都被調用。 只有在視圖初次繪制時,才會調用drawRect方法。要強制更新,可以使用視圖類的 setNeedsDisplay 或者 setNeedsDisplayInRect 方法: [myView setNeedsDisplay]; [myView setNeedsDisplayInRect:self.view]; 八、繪制圖案 如果圖像是一個圖案模板,你可以用UIImage類提供的另外一個方法 drawAsPatternInrect,在整個視圖區域重復繪制該圖像: UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)]; [myImage drawInRect:myView.frame]; [self.view addSubview:myView]; 九、方向 一個圖像的方向,決定了它在屏幕上如何被旋轉。因為iPhone 能被以6種不同的方式握持,所以在方向改變時,能夠將圖像做相應的旋轉就十分必要了。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; 十、圖像尺寸 你可以通過size屬性讀取一個圖像的尺寸,得到一個CGSize 結構,其中包含 wifth 和height 。 CGSize myImageSize = myImage.size; 原文鏈接:http://blog.csdn.net/iukey/article/details/7308433 標簽: <無> 補充話題說明» 分享到 收藏0舉報踩 0 | 頂 0 按默認排序 | 顯示最新評論 | 回頁面頂部
轉:http://blog.csdn.net/iukey/article/details/7308433
為uiimage 繪制 毛玻璃效果
需要導入Accelerate.framework。
#import <Accelerate/Accelerate.h>
//加模糊效果,image是圖片,blur是模糊度 - (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur
{
if (image==nil)
{
NSLog(@"error:為圖片添加模糊效果時,未能獲取原始圖片");
return nil;
}
//模糊度, if ((blur < 0.1f) || (blur > 2.0f)) { blur = 0.5f; } //boxSize必須大於0 int boxSize = (int)(blur * 100); boxSize -= (boxSize % 2) + 1; NSLog(@"boxSize:%i",boxSize); //圖像處理 CGImageRef img = image.CGImage; //需要引入#import <Accelerate/Accelerate.h> /* This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing. 本文檔介紹了Accelerate Framework,其中包含C語言應用程序接口(API)的向量和矩陣數學,數字信號處理,大量處理和圖像處理。 */ //圖像緩存,輸入緩存,輸出緩存 vImage_Buffer inBuffer, outBuffer; vImage_Error error; //像素緩存 void *pixelBuffer; //數據源提供者,Defines an opaque type that supplies Quartz with data. CGDataProviderRef inProvider = CGImageGetDataProvider(img); // provider’s data. CFDataRef inBitmapData = CGDataProviderCopyData(inProvider); //寬,高,字節/行,data inBuffer.width = CGImageGetWidth(img); inBuffer.height = CGImageGetHeight(img); inBuffer.rowBytes = CGImageGetBytesPerRow(img); inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData); //像數緩存,字節行*圖片高 pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); outBuffer.data = pixelBuffer; outBuffer.width = CGImageGetWidth(img); outBuffer.height = CGImageGetHeight(img); outBuffer.rowBytes = CGImageGetBytesPerRow(img); // 第三個中間的緩存區,抗鋸齒的效果 void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img)); vImage_Buffer outBuffer2; outBuffer2.data = pixelBuffer2; outBuffer2.width = CGImageGetWidth(img); outBuffer2.height = CGImageGetHeight(img); outBuffer2.rowBytes = CGImageGetBytesPerRow(img); //Convolves a region of interest within an ARGB8888 source image by an implicit M x N kernel that has the effect of a box filter. error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend); if (error) { NSLog(@"error from convolution %ld", error); } // NSLog(@"字節組成部分:%zu",CGImageGetBitsPerComponent(img)); //顏色空間DeviceRGB CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //用圖片創建上下文,CGImageGetBitsPerComponent(img),7,8 CGContextRef ctx = CGBitmapContextCreate( outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, CGImageGetBitmapInfo(image.CGImage)); //根據上下文,處理過的圖片,重新組件 CGImageRef imageRef = CGBitmapContextCreateImage (ctx); UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; //clean up CGContextRelease(ctx); CGColorSpaceRelease(colorSpace); free(pixelBuffer); free(pixelBuffer2); CFRelease(inBitmapData); //CGColorSpaceRelease(colorSpace); //多余的釋放 CGImageRelease(imageRef); return returnImage; }
參考:http://blog.csdn.net/rhljiayou/article/details/10232003 有效果圖
http://www.zhihu.com/question/21260575 知乎上的討論,比較全面
http://prolove10.blog.163.com/blog/static/138411843201391401054305/
uiimage 轉 nsdata
-(NSData *)getCoverImageDataWith:(NSString *)imgeUrl { NSURL *aUrl = [[NSURL alloc]initWithString:imgeUrl]; NSData *aData = [[NSData alloc]initWithContentsOfURL:aUrl]; if (aData==nil) { aData = [UIImagePNGRepresentation([UIImage imageNamed:@"image_default"]) retain]; } SQRelease(aUrl) return [aData autorelease]; }