Iphone 使用边框框裁剪图像
我正在尝试使用矩形框裁剪图像。但不知何故,我们无法按照要求做到这一点 以下是我正在尝试的: 以下是我想要的结果: 现在我需要的是当点击“完成”图像时,应该将其裁剪成矩形,精确地放置在图像中。我已经尝试了一些事情,比如使用mask image rect屏蔽和绘制图像,但还没有成功 以下是我的代码,它不起作用:Iphone 使用边框框裁剪图像,iphone,objective-c,uiimage,Iphone,Objective C,Uiimage,我正在尝试使用矩形框裁剪图像。但不知何故,我们无法按照要求做到这一点 以下是我正在尝试的: 以下是我想要的结果: 现在我需要的是当点击“完成”图像时,应该将其裁剪成矩形,精确地放置在图像中。我已经尝试了一些事情,比如使用mask image rect屏蔽和绘制图像,但还没有成功 以下是我的代码,它不起作用: CALayer *mask = [CALayer layer]; mask.contents = (id)[imgMaskImage.image CGImage]; mask.frame
CALayer *mask = [CALayer layer];
mask.contents = (id)[imgMaskImage.image CGImage];
mask.frame = imgMaskImage.frame;
imgEditedImageView.layer.mask = mask;
imgEditedImageView.layer.masksToBounds = YES;
有谁能给我建议一个更好的实施方法吗
我尝试了很多其他的事情,浪费了很多时间,所以如果我得到一些帮助,我会非常感激的
谢谢。这是你的做法
+(UIImage *)maskImage:(UIImage *)image andMaskingImage:(UIImage *)maskingImage{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef maskImageRef = [maskingImage CGImage];
CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskingImage.size.width, maskingImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
if (mainViewContentContext==NULL)
return NULL;
CGFloat ratio = 0;
ratio = maskingImage.size.width/ image.size.width;
if(ratio * image.size.height < maskingImage.size.height) {
ratio = maskingImage.size.height/ image.size.height;
}
CGRect rect1 = {{0, 0}, {maskingImage.size.width, maskingImage.size.height}};
//// CHANGE THIS RECT ACCORDING TO YOUR NEEDS
CGRect rect2 = {{-((image.size.width*ratio)-maskingImage.size.width)/2 , -((image.size.height*ratio)-maskingImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);
CGColorSpaceRelease(colorSpace);
UIImage *theImage = [UIImage imageWithCGImage:newImage];
CGImageRelease(newImage);
return theImage;
}
+(UIImage*)maskImage:(UIImage*)image和maskingImage:(UIImage*)maskingImage{
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGImageRef maskImageRef=[maskingImage CGImage];
CGContextRef mainViewContentContext=CGBitmapContextCreate(NULL,maskingImage.size.width,maskingImage.size.height,8,0,颜色空间,KCgimageAlphaPremultipledLast);
if(mainViewContentContext==NULL)
返回NULL;
现金流量比率=0;
比率=maskingImage.size.width/image.size.width;
if(比率*image.size.height
你需要有这样的形象
注意
遮罩图像不能有任何透明度。相反,透明区域必须为白色或介于黑白之间的某个值。像素越接近黑色,其透明度就越低
- (UIImage *)croppedPhoto {
// For dealing with Retina displays as well as non-Retina, we need to check
// the scale factor, if it is available. Note that we use the size of teh cropping Rect
// passed in, and not the size of the view we are taking a screenshot of.
CGRect croppingRect = CGRectMake(imgMaskImage.frame.origin.x,
imgMaskImage.frame.origin.y, imgMaskImage.frame.size.width,
imgMaskImage.frame.size.height);
imgMaskImage.hidden=YES;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES,
[UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(croppingRect.size);
}
// Create a graphics context and translate it the view we want to crop so
// that even in grabbing (0,0), that origin point now represents the actual
// cropping origin desired:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y);
[self.view.layer renderInContext:ctx];
// Retrieve a UIImage from the current image context:
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Return the image in a UIImageView:
return snapshotImage;
}
Check it@Immi我已经用正方形实现了这个东西,但是对于我想要的椭圆形,因为上面的屏幕不工作,这不是我要找的。我知道你到底在找什么,所以,我要求你们上传有问题的单独图像,以及你们得到的输出和你们期望得到的输出,这样人们就会知道出了什么问题