UIImage 影象處理

征途LN發表於2013-12-25

我覺得成為一個IOS開發人員肯定要會的必須要有:圖片、網路、音樂和視訊;

這個專輯就按順序總結一下


1、把UIView 轉化成 UIImage 

-(UIImage*)convertViewToImage:(UIView*)theView
{
    UIGraphicsBeginImageContextWithOptions(theView.bounds.size, YES, theView.layer.contentsScale);
    [theView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

2、更改image的長寬比例

- (UIImage *)setImage:(UIImage *)image toSize:(CGSize)size
{
 UIGraphicsBeginImageContext(CGSizeMake(size.width,size.height));
 [image drawInRect:CGRectMake(0, 0, size.width,size.height)];
 UIImage *scaleImage =UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return scaleImage;
}

3、獲取螢幕截圖

//獲取螢幕截圖的方法從SDK4.0 以後可以使用了

CGImageRef screen = UIGetScreenImage();
UIImage* screenImage = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(screenImage, self, nil, nil);


4、儲存圖片到沙河

NSString * path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile: path atomically: YES];



相關文章