Iphone 使用分割器将图像分成两部分

Iphone 使用分割器将图像分成两部分,iphone,ios,ios6,Iphone,Ios,Ios6,我正在开发一个应用程序,我需要用红线将图像分成两部分 标签的左侧部分 价格的正确部分 问题1. 如何在图像上画红线 问题2. 如何使用红线将图像分割为两部分?(红线位置不固定。用户可以将位置移动到任意位置) 问题3. 我怎样才能得到线的当前位置,我怎样才能使用那个位置来分割图像 提前感谢您的惠顾,您可以试试这个 UIImage *image = [UIImage imageNamed:@"yourImage.png"]; CGImageRef tmpImgRef = image.CGIma

我正在开发一个应用程序,我需要用红线将图像分成两部分

  • 标签的左侧部分

  • 价格的正确部分

  • 问题1.

    如何在图像上画红线

    问题2.

    如何使用红线将图像分割为两部分?(红线位置不固定。用户可以将位置移动到任意位置)

    问题3.

    我怎样才能得到线的当前位置,我怎样才能使用那个位置来分割图像


    提前感谢您的惠顾,您可以试试这个

    UIImage *image = [UIImage imageNamed:@"yourImage.png"];
    CGImageRef tmpImgRef = image.CGImage;
    CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height / 2.0));
    UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
    CGImageRelease(topImgRef);
    
    CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height / 2.0,  image.size.width, image.size.height / 2.0));
    UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
    CGImageRelease(bottomImgRef);
    

    希望这能对你有所帮助,:)

    如果你想画一条线,你可以使用一个红色背景的UIView,将高度设置为图像的大小,宽度设置为5像素左右

        UIView *imageToSplit; //the image im trying to split using a red bar
        CGRect i = imageToSplit.frame;
        int x = i.origin.x + i.size.width/2;
        int y = i.origin.y;
        int width = 5;
        int height = i.size.height;
        UIView *bar = [[[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
        bar.backgroundColor = [UIColor redColor];
        [self.view addSubview:bar];
    

    我将以与科雷所建议的相同的方式来处理这一问题:

    1) 我假设您上面的图像/视图将由视图控制器管理,从现在起我将其称为ImageSeparator或ViewController

    在ImageSeparatorViewController内部,在-(void)viewDidLoad{}方法中插入koray的代码。确保将imageToSplit变量更改为UIImageView而不是普通UIView

    2) 接下来,我假设您知道如何检测用户手势。您将检测到这些手势,并确定用户是否已选择视图(即koray代码中的)。一旦确定用户是否选择了,只需使用触摸位置更新其原点的X位置

    CGRect barFrame = bar.frame; 
    barFrame.origin.x = *X location of the users touch*
    bar.frame = barFrame;
    
    3) 对于裁剪,我不会使用,它不会执行您需要执行的操作。

    请尝试以下尺寸:

    标题:

    @interface UIImage (ImageDivider)
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor*)color;
    - (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage;
    - (NSArray*)imagesBySlicingAt:(CGFloat)position;
    
    @end
    
    @implementation UIImage (ImageDivider)
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage
    {
        //pattern image
        UIColor *patternColor = [UIColor colorWithPatternImage:patternImage];
        CGFloat width = patternImage.size.width;
    
        //set up context
        UIGraphicsBeginImageContext(self.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //draw the existing image into the context
        [self drawAtPoint:CGPointZero];
    
        //set the fill color from the pattern image color
        CGContextSetFillColorWithColor(context, patternColor.CGColor);
    
        //this is your divider's area
        CGRect dividerRect = CGRectMake(position - (width / 2.0f), 0, width, self.size.height);
    
        //the joy of image color patterns being based on 0,0 origin! must set phase
        CGContextSetPatternPhase(context, CGSizeMake(dividerRect.origin.x, 0));
    
        //fill the divider rect with the repeating pattern from the image
        CGContextFillRect(context, dividerRect);
    
        //get your new image and viola!
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor *)color
    {
        //set up context
        UIGraphicsBeginImageContext(self.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //draw the existing image into the context
        [self drawAtPoint:CGPointZero];
    
        //set the fill color for your divider
        CGContextSetFillColorWithColor(context, color.CGColor);
    
        //this is your divider's area
        CGRect dividerRect = CGRectMake(position - (width / 2.0f), 0, width, self.size.height);
    
        //fill the divider's rect with the provided color
        CGContextFillRect(context, dividerRect);
    
        //get your new image and viola!
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    - (NSArray*)imagesBySlicingAt:(CGFloat)position
    {
        NSMutableArray *slices = [NSMutableArray array];
    
        //first image
        {
            //context!
            UIGraphicsBeginImageContext(CGSizeMake(position, self.size.height));
    
            //draw the existing image into the context
            [self drawAtPoint:CGPointZero];
    
            //get your new image and viola!
            [slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
            UIGraphicsEndImageContext();
        }
    
        //second
        {
            //context!
            UIGraphicsBeginImageContext(CGSizeMake(self.size.width - position, self.size.height));
    
            //draw the existing image into the context
            [self drawAtPoint:CGPointMake(-position, 0)];
    
            //get your new image and viola!
            [slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
            UIGraphicsEndImageContext();
        }
        return slices;
    }
    
    实施:

    @interface UIImage (ImageDivider)
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor*)color;
    - (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage;
    - (NSArray*)imagesBySlicingAt:(CGFloat)position;
    
    @end
    
    @implementation UIImage (ImageDivider)
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position patternImage:(UIImage*)patternImage
    {
        //pattern image
        UIColor *patternColor = [UIColor colorWithPatternImage:patternImage];
        CGFloat width = patternImage.size.width;
    
        //set up context
        UIGraphicsBeginImageContext(self.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //draw the existing image into the context
        [self drawAtPoint:CGPointZero];
    
        //set the fill color from the pattern image color
        CGContextSetFillColorWithColor(context, patternColor.CGColor);
    
        //this is your divider's area
        CGRect dividerRect = CGRectMake(position - (width / 2.0f), 0, width, self.size.height);
    
        //the joy of image color patterns being based on 0,0 origin! must set phase
        CGContextSetPatternPhase(context, CGSizeMake(dividerRect.origin.x, 0));
    
        //fill the divider rect with the repeating pattern from the image
        CGContextFillRect(context, dividerRect);
    
        //get your new image and viola!
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    - (UIImage*)imageWithDividerAt:(CGFloat)position width:(CGFloat)width color:(UIColor *)color
    {
        //set up context
        UIGraphicsBeginImageContext(self.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //draw the existing image into the context
        [self drawAtPoint:CGPointZero];
    
        //set the fill color for your divider
        CGContextSetFillColorWithColor(context, color.CGColor);
    
        //this is your divider's area
        CGRect dividerRect = CGRectMake(position - (width / 2.0f), 0, width, self.size.height);
    
        //fill the divider's rect with the provided color
        CGContextFillRect(context, dividerRect);
    
        //get your new image and viola!
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    - (NSArray*)imagesBySlicingAt:(CGFloat)position
    {
        NSMutableArray *slices = [NSMutableArray array];
    
        //first image
        {
            //context!
            UIGraphicsBeginImageContext(CGSizeMake(position, self.size.height));
    
            //draw the existing image into the context
            [self drawAtPoint:CGPointZero];
    
            //get your new image and viola!
            [slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
            UIGraphicsEndImageContext();
        }
    
        //second
        {
            //context!
            UIGraphicsBeginImageContext(CGSizeMake(self.size.width - position, self.size.height));
    
            //draw the existing image into the context
            [self drawAtPoint:CGPointMake(-position, 0)];
    
            //get your new image and viola!
            [slices addObject:UIGraphicsGetImageFromCurrentImageContext()];
            UIGraphicsEndImageContext();
        }
        return slices;
    }
    
    这个概念很简单——你想要一个画有分割线的图像。您可以只覆盖一个视图,或覆盖
    drawRect:
    ,或任何数量的解决方案。我宁愿给你这个类别。它只需使用一些快速核心图形调用,在指定位置生成具有所需分割线的图像,无论是图案图像还是颜色。如果您还希望支持水平分隔符,那么将其修改为水平分隔符相当简单奖励:您可以使用平铺图像作为分割线

    现在回答您的主要问题。使用这个类别是很自然的——只需在源背景上调用两个方法中的一个,用分割器生成一个,然后应用该图像而不是原始源图像

    现在,第二个问题很简单-移动分割器后,根据新的分割器位置重新生成图像。这实际上是一种效率相对较低的方法,但对于您的目的来说,这应该足够轻,并且仅在移动分隔器时是一个问题。过早优化也是一种罪恶

    第三个问题也很简单-调用
    imagesBySlicingAt:
    -它将返回一个由两个图像组成的数组,这两个图像是通过在提供的位置对图像进行切片生成的。随心所欲地使用它们


    此代码已通过功能测试。我强烈建议您随意使用它,不是出于任何实用目的,而是为了更好地理解所使用的机制,以便下次您能够回答问题

    谢谢您的回答,但我已经使用了此代码,它工作得很好。如何在图像上添加红线,并使用红线分割图像。任何参考或想法。这可能对你有帮助。但是你必须处理它来管理你的跳线。我尝试了BFCropInterface和BJImageCropper,但是这两种方法都是裁剪图像而不是分割图像。这很好。这对我很有帮助。非常感谢。