【问题标题】:Cropping image in iPhone在 iPhone 中裁剪图像
【发布时间】:2011-09-12 09:36:33
【问题描述】:

我喜欢在 UIImageView 中裁剪图像。您能否提供对我有帮助的完整源代码。

【问题讨论】:

    标签: iphone crop


    【解决方案1】:
    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    StartPoint = [touch locationInView:touch.view];
    if (Img_Screen!=nil) {
        [Img_Screen removeFromSuperview];
        Img_Screen=nil;
    
    }
    

    }

     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    EndPoint = [touch locationInView:touch.view];
    [self finishedTouchInside];
    

    }

     -(void)finishedTouchInside{
    
    CGFloat width=EndPoint.x-StartPoint.x;
    CGFloat hieght=EndPoint.y-StartPoint.y;
    CGRect myImageRect = CGRectMake(StartPoint.x, StartPoint.y, width, hieght);
    Img_Screen= [[UIImageView alloc] initWithFrame:myImageRect];
    
    CGImageRef imageRef = CGImageCreateWithImageInRect([imgView.image CGImage], myImageRect);
    // or use the UIImage wherever you like
    [Img_Screen setImage:[UIImage imageWithCGImage:imageRef]];
    CGImageRelease(imageRef);
    [self.view addSubview:Img_Screen];
    imageViewNew.image=[UIImage imageWithCGImage:imageRef];
    

    }

    这里你可以在 imageView 中进行裁剪,裁剪后的图像将显示在 ImageViewNew 中

    【讨论】:

      【解决方案2】:

      如果您需要 UI 控件来裁剪图像,请尝试 SSPhotoCropperViewController。它是一个自定义视图控制器,提供简单、可配置且易于使用的 UI,用于在 iPhone 应用程序中裁剪和缩放照片。

      这里是tutorialsource code on GitHub

      【讨论】:

        【解决方案3】:

        我曾经在同一个 UIImageview 中裁剪图像

        1)存储图片uiimageview

        2)取同一个uiimageview,存为Uiimage

        3)裁剪图像并恢复它

        下面的代码会有所帮助

        -(IBAction)setCropItem:(id)sender {

        UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *finishedPicForCrop = UIGraphicsGetImageFromCurrentImageContext();
        CGImageRef imageRef = CGImageCreateWithImageInRect([finishedPicForCrop CGImage], CGRectMake(0, 45, 320, 420));
        UIImage *img = [UIImage imageWithCGImage:imageRef]; 
        CGImageRelease(imageRef);
        [firstImageView removeFromSuperview];
        secondImageView = [[UIImageView alloc] initWithImage:img];
        [secondImageView setFrame:CGRectMake(0, 0, 320, 460)];
        scrollView.contentSize = secondImageView.bounds.size;
        [scrollView addSubview:secondImageView];
        

        }

        【讨论】:

          【解决方案4】:

          可能对你有帮助

           UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300
          
          
           CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
            UIImage* part = [UIImage imageWithCGImage:cgImg];
            UIImageView* Croppedimage = [[UIImageView alloc] initWithImage:part];
          

          下面是更有用的链接

          how to crop image in to pieces programmatically

          祝你好运

          【讨论】:

          • 感谢您的建议,我终于在上周六找到了答案。它与上面的代码相同,我使用 CGImageCreateWithImageInRect() 来裁剪图像,但我曾经将 uiimageview 存储为 uiimage,然后在同一个 uiimageview 中裁剪图像
          猜你喜欢
          • 1970-01-01
          • 2011-12-18
          • 1970-01-01
          • 2011-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-02
          • 2012-09-22
          相关资源
          最近更新 更多