关于UIImage的一些知识

0,关于初始化

使用ImageNamed方法,类方法,系统有缓存

autorealease

使NSData的话,系统不会有缓存,这样作者可以很好的控制系统内存

 

 

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
只要是继承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.储存图片
储存图片这里分成储存到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.0後apple就開放了

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

你可能感兴趣的:(关于UIImage的一些知识)