ios 裁剪框大小_iOS选择图片自定义系统裁剪框

本文介绍了如何在iOS应用中实现自定义图片裁剪框,以达到特定比例的效果,如微博封面图的375:280比例。通过修改UIImagePickerController的代理方法和利用类别扩展UIImage来实现裁剪图片到所需比例。
摘要由CSDN通过智能技术生成

使用场景举例,玩过微博发表长文章的应该都知道,可以选择封面图,这个封面图是有一定比例的,例如375/280,那我们怎么控制我们选择出来的图片一定是这比例呢,那就是截取这一比例的部分图片,有兴趣的朋友可以去微博看一下效果,下面我们来说一下实现方案.

需要调用的相册的控制器内代码

/**注意!控制器需实现相应代理*/

-(void)chooseImg {

UIImagePickerController *pc = [[UIImagePickerController alloc]init];

pc.delegate = self;

[pc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

[pc setModalPresentationStyle:UIModalPresentationFullScreen];

[pc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

[pc setAllowsEditing:YES];

[self presentViewController:pc animated:YES completion:nil];

}

//实现此方法,给系统相册增加方法

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

if (navigationController.viewControllers.count == 3)

{

Method method = class_getInstanceMethod([self class], @selector(drawRect:));

class_replaceMethod([[[[navigationController viewControllers][2].view subviews][1] subviews][0] class],@selector(drawRect:),method_getImplementation(method),method_getTypeEncoding(method));

}

}

//我们需要的截取框(比例根据实际需要设置,此处为 375:280)

-(void)drawRect:(CGRect)rect

{

CGContextRef ref = UIGraphicsGetCurrentContext();

CGContextAddRect(ref, rect);

CGFloat h = (APP_WIDTH * 280) / 375;

CGContextAddRect(ref, CGRectMake(0, (APP_HEIGHT - h) * 0.5 , APP_WIDTH, h));

[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]setFill];

CGContextDrawPath(ref, kCGPathEOFill);

CGContextAddRect(ref, CGRectMake(0, (APP_HEIGHT - h) * 0.5 , APP_WIDTH, h));

[[UIColor whiteColor]setStroke];

CGContextStrokePath(ref);

}

//当得到照片或者视频后,调用该方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

NSLog(@"Picker returned successfully.");

NSLog(@"%@", info);

if (_isHeadClick) {

NSLog(@"----%s",__func__);

[picker dismissViewControllerAnimated:YES completion:nil];

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

NSLog(@"==image==%@",NSStringFromCGSize(image.size));

//生成最终图片

UIImage *image2 = [UIImage imagewithImage:image];

}

}

UIImage+cutImage

.h文件

#import

@interface UIImage (cutImage)

//将图片截成方形图片

+ (UIImage *)imagewithImage:(UIImage *)image;

@end

.m文件

#import "UIImage+cutImage.h"

@implementation UIImage (cutImage)

+ (UIImage *)imagewithImage:(UIImage *)image

{

CGFloat width = image.size.width;

//因为实际的截图还是系统的剪切框截出来的图片,所以这里处理成我们自己实际需要的比例

CGFloat resultH = width * 280 / 375;

CGFloat height = image.size.height;

CGRect rect = CGRectMake(0, (height - resultH) * 0.5, width, resultH);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);

UIImage *thumbScale = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return thumbScale;

}

@end

Untitled.gif

ps:个人觉得还不够完美,如有更完美方案还请告知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值