UIImage 圖片處理:截圖,縮放,設(shè)定大小,存儲(chǔ)

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.自定長(zhǎng)寬

- (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.處理某個(gè)特定View
只要是繼承UIView的object 都可以處理
必須先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.儲(chǔ)存圖片
儲(chǔ)存圖片這里分成儲(chǔ)存到app的文件里和儲(chǔ)存到手機(jī)的圖片庫(kù)里

  1. 儲(chǔ)存到app的文件里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];

把要處理的圖片, 以image.png名稱存到app home下的Documents目錄里
2)儲(chǔ)存到手機(jī)的圖片庫(kù)里(必須在真機(jī)使用,模擬器無(wú)法使用)

CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
UIGetScreenImage(); // 原來(lái)是private(私有)api, 用來(lái)截取整個(gè)畫(huà)面,不過(guò)SDK 4.0后apple就開(kāi)放了

轉(zhuǎn)載:http://blog.csdn.net/xuhuan_wh/article/details/6434055
SDWebImage:異步緩存加載網(wǎng)絡(luò)圖片 http://hao.jobbole.com/sdwebimage-ios/?utm_source=hao.jobbole.com&utm_medium=relatedResources

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

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

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