图片简单处理(尺寸变换)

图片拉伸和尺寸变换

  • 图片拉伸
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
一般leftCapWidth = imageWidth *.5f, topCapHeight = imageWidth * .5f;
//尺寸变换:
//resize图片
- (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;
}

图片的处理大概分 截图(capture), 缩放(scale), 设定大小(resize), 存储(save)

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就开放了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • #pragma mark 储存图片 - (void)saveImage:(UIImage *)image { //...
    翘起地球表面阅读 4,785评论 0 2
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,409评论 4 61
  • 每个教过学霸小妞的老师都会说同一句话:给 ××× 上课真的是很开心啊!要是每个学生都像她那么乖,那么自觉就好了。 ...
    EstherD阅读 1,526评论 0 0
  • 已经记不太清高考时的场景,但又清楚明白自己高中浑浑噩噩,大概就是现在所说的看起来很努力。 每天沉迷幻想中盲目自信自...
    山眉水眼阅读 1,081评论 0 0
  • 企业管理中,简单管理指的是遵循人性的规律,站在企业长远发展的高度,通过顺应人性的机制设计,从根本上激励员工,实现员...
    积分制管理阅读 2,972评论 0 0

友情链接更多精彩内容