简体   繁体   English

在放置在UIImageview上的矩形框架中创建图像

[英]Creating image with in the frame of a rectangle placed on a UIImageview

How to create a new image with specified frame in a UIView, the view contains imageviews and buttons also.I also need to get the image in the specified frame as a new image. 如何在UIView中创建具有指定框架的新图像,该视图还包含图像视图和按钮。我还需要将指定框架中的图像作为新图像获取。 I've tried this but it not correct. 我已经尝试过了,但是不正确。

I need to crop from UIView and make it as a new UIImage. 我需要从UIView裁剪并使其成为新的UIImage。

You need to import this framework in your project. 您需要在项目中导入此框架。

#import <QuartzCore/QuartzCore.h>

And use following code 并使用以下代码

[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0]; // set border width as per your requirement.

EDITED: 编辑:

Use any one which you need. 使用您需要的任何一个。

For Get Crop Image: 对于获取作物图像:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

Use following method that return UIImage ( as You want size of image ) 使用以下方法返回UIImage根据需要的图像大小

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

Here you get Croped Image that return by above method; 在这里,您将获得通过上述方法返回的裁剪图像;

OR RESIZING 或调整

And also Use following method for specific hight and width with image for Resizing UIImage : 还可以使用以下方法对图像的特定高度宽度进行调整UIImage大小

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

This method return NewImage , with specific size that you want. 此方法返回NewImage ,具有所需的特定大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM