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

theFeng-發表於2014-10-17

圖片的處理大概就分 截圖(capture), 縮放(scale),設定大小(resize), 儲存(save)

這幾樣比較好處理, 另外還有濾鏡,擦試等, 以後再說

在這個Demo code, 我寫了幾個方法


1.等比率縮放

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

                                }

                                

 2.自定長寬

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

    }

                                

 3.處理某個特定View

只要是繼承UIViewobject 都可以處理 必須先import QuzrtzCore.framework

                                

 -(UIImage*)captureView:(UIView *)theView

    {

        CGRect rect = theView.frame;

        UIGraphicsBeginImageContext(rect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        [theView.layer renderInContext:context];

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        

        return img;

    }

                                

  4.儲存圖片

儲存圖片這裡分成儲存到app的檔案裡, 儲存到手機的圖片庫裡

1) 儲存到app的檔案裡

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

    [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];

    這樣就把你要處理的圖片, image.png這個檔名存到app home底下的Documents目錄裡

2)儲存到手機的圖片庫裡

    CGImageRef screen = UIGetScreenImage();

    UIImage* image = [UIImage imageWithCGImage:screen];

    CGImageRelease(screen);

    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

    UIGetScreenImage()原本是private(私有)api, 用來擷取整個畫麵

    不過SDK 4.0apple就開放了

    另外儲存到手機的圖片庫裡, 必須在實機使用, 模擬器無法使用

    

    以下程式碼用到了Quartz FrameworkCore Graphics Framework. workspaceframework目錄裡新增這兩個framework.UIKit裡,影象類UIImageCGImageRef的畫圖操作都是通過Graphics Context來完成。Graphics Context封裝了變換的引數,使得在不同的坐標係裡操作影象非常方便。缺點就是,獲取影象的資料不是那麼方便。下麵會給出獲取資料區的程式碼。

    

    

    

    UIView中獲取影象相當於視窗截圖。ios提供全域性的全屏截圖函式UIGetScreenView(). 如果需要特定區域的影象,可以crop一下。

    CGImageRef screen = UIGetScreenImage();

    UIImage* image = [UIImage imageWithCGImage:screen];

    對於特定UIView的截圖,可以把當前Viewlayer,輸出到一個ImageContext中,然後利用這個ImageContext得到UIImage

    -(UIImage*)captureView: (UIView *)theView

    {

        CGRect rect = theView.frame;

        UIGraphicsBeginImageContext(rect.size);

        CGContextRef context =UIGraphicsGetCurrentContext();

        [theView.layer renderInContext:context];

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        

        return img;

    }

    如果需要裁剪製定區域,可以path & clip,以下例子是建一個200x200的影象上下文,再擷取出左上角

    UIGraphicsBeginImageContext(CGMakeSize(200,200));

    CGContextRefcontext=UIGraphicsGetCurrentContext();

    UIGraphicsPushContext(context);

    // ...把圖寫到context中,省略[indent]CGContextBeginPath();

    CGContextAddRect(CGMakeRect(0,0,100,100));

    CGContextClosePath();[/indent]CGContextDrawPath();

    CGContextFlush(); // 強制執行上面定義的操作

    UIImage* image = UIGraphicGetImageFromCurrentImageContext();

    UIGraphicsPopContext();

    儲存影象分為儲存到home目錄檔案和圖片庫檔案。儲存到目錄檔案是這樣

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

    [UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

    若要儲存到圖片庫裡面

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    

    UImage封裝了CGImage, 互相轉換很容易

    UIImage* imUI=nil;

    CGImageRef imCG=nil;

    imUI = [UIImage initWithCGImage:imCG];

    imCG = imUI.CGImage;

    CGImage上獲取影象資料區,在apple dev上有QA, 不過好像還不支援ios

    下麵給出一個在ios上反色的例子

    -(id)invertContrast:(UIImage*)img

    {

        CGImageRef inImage = img.CGImage;

        CGContextRef ctx;

        CFDataRef m_DataRef;

        m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));

        

        int width = CGImageGetWidth( inImage );

        int height = CGImageGetHeight( inImage );

        

        int bpc = CGImageGetBitsPerComponent(inImage);

        int bpp = CGImageGetBitsPerPixel(inImage);

        int bpl = CGImageGetBytesPerRow(inImage);

        

        UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);

        int length = CFDataGetLength(m_DataRef);

        

        NSLog(@"len %d", length);

        NSLog(@"width=%d, height=%d", width, height);

        NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);

        

        for (int index = 0; index < length; index += 4)

        { 

            m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b

            m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g

            m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r

        }

        

        ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );

        CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

        UIImage* rawImage = [UIImage imageWithCGImage:imageRef];

        CGContextRelease(ctx);

        return rawImage;

    }

    

    得到影象資料區後就可以很方便的實現影象處理的演算法。下麵給顯示影象資料區的方法,也就是unsigned char*轉為graphics context或者UIImage或和CGImageRef

    CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );

    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

    UIImage* image = [UIImage imageWithCGImage:imageRef];

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

    [UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];

    CGContextRelease(ctx);

    關於影象獲取方麵,在這裡應該都覆蓋到了,不正之處,歡迎指正。

相關文章