UIImage 圖片處理:截圖,縮放,設定大小,存儲

  1 圖片的處理大概分 截圖(capture),  縮放(scale), 設定大小(resize),  存儲(save)
  2 
  3 
  4 1.等比率縮放
  5 - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
  6 
  7 {
  8 
  9 UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
 10 [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
 11 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 12 UIGraphicsEndImageContext();
 13 
 14 return scaledImage;
 15 
 16 }
 17 
 18 
 19 2.自定長寬
 20 - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
 21 
 22 {
 23 UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
 24 [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
 25 UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
 26 UIGraphicsEndImageContext();
 27 
 28 return reSizeImage;
 29 
 30 }
 31 
 32 
 33 3.處理某個特定View
 34 只要是繼承UIView的object 均可以處理
 35 必須先import QuzrtzCore.framework
 36 
 37 
 38 -(UIImage*)captureView:(UIView *)theView
 39 
 40 {
 41 CGRect rect = theView.frame;
 42 UIGraphicsBeginImageContext(rect.size);
 43 CGContextRef context = UIGraphicsGetCurrentContext();
 44 [theView.layer renderInContext:context];
 45 UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
 46 UIGraphicsEndImageContext();
 47 
 48 return img;
 49 
 50 }
 51 
 52 
 53 4.儲存圖片
 54 儲存圖片這裏分紅儲存到app的文件裏和儲存到手機的圖片庫裏
 55 
 56 1) 儲存到app的文件裏
 57 NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
 58 [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
 59 把要處理的圖片, 以image.png名稱存到app home下的Documents目錄裏
 60 
 61 2)儲存到手機的圖片庫裏(必須在真機使用,模擬器沒法使用)
 62 CGImageRef screen = UIGetScreenImage();
 63 UIImage* image = [UIImage imageWithCGImage:screen];
 64 CGImageRelease(screen);
 65 UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
 66 UIGetScreenImage(); // 原來是private(私有)api, 用來截取整個畫面,不過SDK 4.0後apple就開放了
 67 
 68 //====================================================================================
 69 
 70 如下代碼用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目錄裏添加這兩個framework.在UIKit裏,圖像類UIImage和CGImageRef的畫圖操做都是經過Graphics Context來完成。Graphics Context封裝了變換的參數,使得在不一樣的座標系裏操做圖像很是方便。缺點就是,獲取圖像的數據不是那麼方便。下面會給出獲取數據區的代碼。
 71 
 72  
 73 
 74 1. 從UIView中獲取圖像至關於窗口截屏。
 75 
 76 (ios提供全局的全屏截屏函數UIGetScreenView(). 若是須要特定區域的圖像,能夠crop一下)
 77 
 78     CGImageRef screen = UIGetScreenImage();
 79     UIImage* image = [UIImage imageWithCGImage:screen];
 80 
 81 2. 對於特定UIView的截屏。
 82 
 83 (能夠把當前View的layer,輸出到一個ImageContext中,而後利用這個ImageContext獲得UIImage)
 84 
 85     -(UIImage*)captureView: (UIView *)theView
 86     {
 87     CGRect rect = theView.frame;
 88     UIGraphicsBeginImageContext(rect.size);
 89     CGContextRef context =UIGraphicsGetCurrentContext();
 90     [theView.layer renderInContext:context];
 91     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
 92     UIGraphicsEndImageContext();
 93 
 94     return img;
 95     }
 96 
 97 3. 若是須要裁剪指定區域。
 98 
 99 (能夠path & clip,如下例子是建一個200x200的圖像上下文,再截取出左上角)
100 
101     UIGraphicsBeginImageContext(CGMakeSize(200,200));
102     CGContextRefcontext=UIGraphicsGetCurrentContext();
103     UIGraphicsPushContext(context);
104     // ...把圖寫到context中,省略[indent]CGContextBeginPath();
105     CGContextAddRect(CGMakeRect(0,0,100,100));
106     CGContextClosePath();[/indent]CGContextDrawPath();
107     CGContextFlush(); // 強制執行上面定義的操做
108     UIImage* image = UIGraphicGetImageFromCurrentImageContext();
109     UIGraphicsPopContext();
110 
111 4. 存儲圖像。
112 
113 (分別存儲到home目錄文件和圖片庫文件。)
114 
115 存儲到目錄文件是這樣
116 
117     NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
118     [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
119 
120 若要存儲到圖片庫裏面
121 
122     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
123 
124 
125 5.  互相轉換UImage和CGImage。
126 
127 (UImage封裝了CGImage, 互相轉換很容易)
128 
129     UIImage* imUI=nil;
130     CGImageRef imCG=nil;
131     imUI = [UIImage initWithCGImage:imCG];
132     imCG = imUI.CGImage;
133 
134 6. 從CGImage上獲取圖像數據區。
135 
136 (在apple dev上有QA, 不過好像還不支持ios)
137 
138 
139 下面給出一個在ios上反色的例子
140 
141     -(id)invertContrast:(UIImage*)img
142     {
143     CGImageRef inImage = img.CGImage; 
144     CGContextRef ctx;
145     CFDataRef m_DataRef;
146     m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
147 
148     int width = CGImageGetWidth( inImage );
149     int height = CGImageGetHeight( inImage );
150 
151     int bpc = CGImageGetBitsPerComponent(inImage);
152     int bpp = CGImageGetBitsPerPixel(inImage);
153     int bpl = CGImageGetBytesPerRow(inImage);
154 
155     UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
156     int length = CFDataGetLength(m_DataRef);
157 
158     NSLog(@"len %d", length);
159     NSLog(@"width=%d, height=%d", width, height);
160     NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);
161 
162     for (int index = 0; index < length; index += 4)
163     { 
164     m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
165     m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
166     m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
167     }
168 
169     ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
170     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
171     UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
172     CGContextRelease(ctx);
173     return rawImage;
174     }
175 
176  
177 
178 7. 顯示圖像數據區。
179 
180 (顯示圖像數據區,也就是unsigned char*轉爲graphics context或者UIImage或和CGImageRef)
181 
182     CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
183     CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
184     UIImage* image = [UIImage imageWithCGImage:imageRef];
185     NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
186     [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
187     CGContextRelease(ctx);
188 
189 獲得圖像數據區後就能夠很方便的實現圖像處理的算法
相關文章
相關標籤/搜索