如何在 iPhone 中裁剪图像

发布于 2024-12-13 03:33:12 字数 16 浏览 0 评论 0原文

我想做同样的事情

I want to do the same thing as asked in this question.
In my App i want to crop the image like we do image cropping in FaceBook can any one guide me with the link of good tutorial or with any sample code. The Link which i have provided will completely describe my requirement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-12-20 03:33:12

您可以创建具有任何属性的新图像。这是我的功能,女巫执行此操作。您只需要使用自己的新图像参数即可。就我而言,图像没有被裁剪,我只是做了一些效果,将像素从原始位置移动到另一个位置。但是,如果您使用另一个高度和宽度初始化新图像,则可以将所需的旧图像的任何像素范围复制到新图像:

-(UIImage *)Color:(UIImage *)img
{
    int R;
    float m_width = img.size.width;
    float m_height = img.size.height;
    if (m_width>m_height) R = m_height*0.9;
    else R = m_width*0.9;
    int m_wint = (int)m_width;      //later,  we will need this parameters in float and int. you may just use "(int)" and "(float)" before variables later, and do not implement another ones
    int m_hint = (int)m_height;

    CGRect imageRect;
    //cheking image orientation. we will work with image pixel-by-pixel, so we need to make top side at the top.
    if(img.imageOrientation==UIImageOrientationUp 
       || img.imageOrientation==UIImageOrientationDown) 
    {
        imageRect = CGRectMake(0, 0, m_wint, m_hint); 
    }
    else 
    {
        imageRect = CGRectMake(0, 0, m_hint, m_wint); 
    }
    uint32_t *rgbImage = (uint32_t *) malloc(m_wint * m_hint * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImage, m_wint, m_hint, 8, m_wint *sizeof(uint32_t), colorSpace,   kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextTranslateCTM(context, 0, m_hint);
    CGContextScaleCTM(context, 1.0, -1.0);
    switch (img.imageOrientation) {
        case UIImageOrientationRight:
        {
            CGContextRotateCTM(context, M_PI / 2);
            CGContextTranslateCTM(context, 0, -m_wint);            
        }break;
        case UIImageOrientationLeft:
        {
            CGContextRotateCTM(context, - M_PI / 2);
            CGContextTranslateCTM(context, -m_hint, 0);            
        }break;
        case UIImageOrientationUp:
        {
            CGContextTranslateCTM(context, m_wint, m_hint);
            CGContextRotateCTM(context, M_PI);
        }
        default:
            break;
    }

    CGContextDrawImage(context, imageRect, img.CGImage);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    //here is new image. you can change m_wint and m_hint as you whant
    uint8_t *result = (uint8_t *) calloc(m_wint * m_hint * sizeof(uint32_t), 1);
    for(int y = 0; y < m_hint; y++) //new m_hint here
    {
        float fy=y;
        double yy =    (m_height*(  asinf(m_height/(2*R))-asin(((m_height/2)-fy)/R)   )) /
        (2*asin(m_height/(2*R))); // (xx, yy) - coordinates of pixel of OLD image
        for(int x =  0; x < m_wint; x++) //new m_wint here
        {
            float fx=x;
            double xx =    (m_width*(  asin(m_width/(2*R))-asin(((m_width/2)-fx)/R)   )) /
            (2*asin(m_width/(2*R)));
            uint32_t rgbPixel=rgbImage[(int)yy * m_wint + (int)xx];
            int intRedSource = (rgbPixel>>24)&255;
            int intGreenSource = (rgbPixel>>16)&255;
            int intBlueSource = (rgbPixel>>8)&255;
            result[(y * (int)m_wint + x) * 4] = 0;
            result[(y * (int)m_wint + x) * 4 + 1] = intBlueSource;
            result[(y * (int)m_wint + x) * 4 + 2] = intGreenSource;
            result[(y * (int)m_wint + x) * 4 + 3] = intRedSource;

        }
    }

    free(rgbImage);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(result, m_wint, m_hint, 8, m_wint * sizeof(uint32_t), colorSpace,   kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast  ); //new m_wint and m_hint as well


    CGImageRef image1 = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *resultUIImage = [UIImage imageWithCGImage:image1];
    CGImageRelease(image1);


    @try {
        free(result);
    }
    @catch (NSException * e) {
        NSLog(@"proc. Exception: %@", e);
    }

    return resultUIImage;
}

You may create new image with any properties. Here is my function, witch do that. you just need to use your own parameters of new image. In my case, image is not cropped, I just making some effect, moving pixels from there original place to another. But if you initialize new image with another height and width, you can just copy from any range of pixels of old image you need, to new one:

-(UIImage *)Color:(UIImage *)img
{
    int R;
    float m_width = img.size.width;
    float m_height = img.size.height;
    if (m_width>m_height) R = m_height*0.9;
    else R = m_width*0.9;
    int m_wint = (int)m_width;      //later,  we will need this parameters in float and int. you may just use "(int)" and "(float)" before variables later, and do not implement another ones
    int m_hint = (int)m_height;

    CGRect imageRect;
    //cheking image orientation. we will work with image pixel-by-pixel, so we need to make top side at the top.
    if(img.imageOrientation==UIImageOrientationUp 
       || img.imageOrientation==UIImageOrientationDown) 
    {
        imageRect = CGRectMake(0, 0, m_wint, m_hint); 
    }
    else 
    {
        imageRect = CGRectMake(0, 0, m_hint, m_wint); 
    }
    uint32_t *rgbImage = (uint32_t *) malloc(m_wint * m_hint * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImage, m_wint, m_hint, 8, m_wint *sizeof(uint32_t), colorSpace,   kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextTranslateCTM(context, 0, m_hint);
    CGContextScaleCTM(context, 1.0, -1.0);
    switch (img.imageOrientation) {
        case UIImageOrientationRight:
        {
            CGContextRotateCTM(context, M_PI / 2);
            CGContextTranslateCTM(context, 0, -m_wint);            
        }break;
        case UIImageOrientationLeft:
        {
            CGContextRotateCTM(context, - M_PI / 2);
            CGContextTranslateCTM(context, -m_hint, 0);            
        }break;
        case UIImageOrientationUp:
        {
            CGContextTranslateCTM(context, m_wint, m_hint);
            CGContextRotateCTM(context, M_PI);
        }
        default:
            break;
    }

    CGContextDrawImage(context, imageRect, img.CGImage);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    //here is new image. you can change m_wint and m_hint as you whant
    uint8_t *result = (uint8_t *) calloc(m_wint * m_hint * sizeof(uint32_t), 1);
    for(int y = 0; y < m_hint; y++) //new m_hint here
    {
        float fy=y;
        double yy =    (m_height*(  asinf(m_height/(2*R))-asin(((m_height/2)-fy)/R)   )) /
        (2*asin(m_height/(2*R))); // (xx, yy) - coordinates of pixel of OLD image
        for(int x =  0; x < m_wint; x++) //new m_wint here
        {
            float fx=x;
            double xx =    (m_width*(  asin(m_width/(2*R))-asin(((m_width/2)-fx)/R)   )) /
            (2*asin(m_width/(2*R)));
            uint32_t rgbPixel=rgbImage[(int)yy * m_wint + (int)xx];
            int intRedSource = (rgbPixel>>24)&255;
            int intGreenSource = (rgbPixel>>16)&255;
            int intBlueSource = (rgbPixel>>8)&255;
            result[(y * (int)m_wint + x) * 4] = 0;
            result[(y * (int)m_wint + x) * 4 + 1] = intBlueSource;
            result[(y * (int)m_wint + x) * 4 + 2] = intGreenSource;
            result[(y * (int)m_wint + x) * 4 + 3] = intRedSource;

        }
    }

    free(rgbImage);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate(result, m_wint, m_hint, 8, m_wint * sizeof(uint32_t), colorSpace,   kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast  ); //new m_wint and m_hint as well


    CGImageRef image1 = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIImage *resultUIImage = [UIImage imageWithCGImage:image1];
    CGImageRelease(image1);


    @try {
        free(result);
    }
    @catch (NSException * e) {
        NSLog(@"proc. Exception: %@", e);
    }

    return resultUIImage;
}
送君千里 2024-12-20 03:33:12

CGRect rectImage = CGRectMake(p1.x,p1.y, p2.x - p1.x, p4.y - p1.y);

//Create bitmap image from original image data,
//using rectangle to specify desired crop area

CGImageRef imageRef = CGImageCreateWithImageInRect([imageForCropping CGImage], rectImage); 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(p1.x, p1.y,p2.x-p1.x p4.y-p1.y)];
imageView1.image = croppedImage;
[self.view addSubview:imageView1];
CGImageRelease(imageRef);

CGRect rectImage = CGRectMake(p1.x,p1.y, p2.x - p1.x, p4.y - p1.y);

//Create bitmap image from original image data,
//using rectangle to specify desired crop area

CGImageRef imageRef = CGImageCreateWithImageInRect([imageForCropping CGImage], rectImage); 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(p1.x, p1.y,p2.x-p1.x p4.y-p1.y)];
imageView1.image = croppedImage;
[self.view addSubview:imageView1];
CGImageRelease(imageRef);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文