Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何裁剪具有特定位置的图像并设置为其他视图_Ios_Objective C - Fatal编程技术网

Ios 如何裁剪具有特定位置的图像并设置为其他视图

Ios 如何裁剪具有特定位置的图像并设置为其他视图,ios,objective-c,Ios,Objective C,从特定位置裁剪图像并设置为其他视图 最终图像视图 self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)]; if(rectangle_button_preesed_view) { self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(3

从特定位置裁剪图像并设置为其他视图

最终图像视图

self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)];
if(rectangle_button_preesed_view)
{
    self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 120, 260, 340)];
}
else
{
    self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 80, 260, 260)];
}
裁剪图像

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    NSLog(@" cropped size %f %f ",cropped.size.width,cropped.size.height);
    return cropped;    
}


除了
CoreGraphics
解决方案之外,我还建议使用
CoreImage
支持图像处理过滤器。您应该查看中的
CICrop
过滤器

注意:iOS 5或更高版本支持此筛选器

我目前没有一个确切的解决方案,但你可以继续

希望有帮助

@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)cropRect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
    UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropedImage;

}
@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)rect {

    rect = CGRectMake(rect.origin.x*self.scale, 
                      rect.origin.y*self.scale, 
                      rect.size.width*self.scale, 
                      rect.size.height*self.scale);       

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef 
                                          scale:self.scale 
                                    orientation:self.imageOrientation]; 
    CGImageRelease(imageRef);
    return result;
}

@end
@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)cropRect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
    UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropedImage;

}