在放置在UIImageview上的矩形框架中创建图像 [英] Creating image with in the frame of a rectangle placed on a UIImageview

查看:93
本文介绍了在放置在UIImageview上的矩形框架中创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在UIView中创建具有指定帧的新图像,该视图还包含图像视图和按钮。我还需要将指定帧中的图像作为新图像。
我试过这个但是不正确。

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. I've tried this but it not correct.

我需要从UIView裁剪并将其作为新的UIImage。

I need to crop from UIView and make it as a new UIImage.

推荐答案

您需要在项目中导入此框架。

You need to import this framework in your project.

#import <QuartzCore/QuartzCore.h>

并使用以下代码

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

已编辑:

使用你需要的任何一个。

Use any one which you need.

获取裁剪图像:

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

使用以下方法返回 UIImage as你想要的图像大小

- (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;
    }

在这里你得到通过上述方法返回的Croped Image;

Here you get Croped Image that return by above method;

或重新调整

并且还针对特定的 hight 使用以下方法图片 图片 调整UIImage的大小

And also Use following method for specific hight and width with image for Resizing 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;
}

此方法返回 NewImage ,具有特定大小你要。

This method return NewImage, with specific size that you want.

这篇关于在放置在UIImageview上的矩形框架中创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆