【问题标题】:iOS imageview crop not working when work with device photos处理设备照片时,iOS imageview 裁剪不起作用
【发布时间】:2015-06-21 00:52:26
【问题描述】:

我计划创建一个图像编辑应用程序。第一步,我将触摸的图像位置显示到与原始图像视图不同的图像视图中。

使用 默认图片(从 xcode storyboard 属性检查器设置)测试时效果很好。

但当我从设备“照片”导入照片时,它不会裁剪精确的图像。

我真的很困惑并坚持在那里。请有人指导我完成这项任务。

我尝试了下面的代码

提前致谢。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    imgVw.image = image;

//    croperImgvw.image = [self cropImage : image];

    [self dismissViewControllerAnimated:YES completion:NULL];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}



- (UIImage *)cropImage:(UIImage *)image : (CGPoint)point
{

    CGRect clippedRect =CGRectMake(point.x, point.y, 50, 50);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
    UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;


}



-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];

    CGPoint touch_point = [touch locationInView:self.view];


    NSLog(@"X location: %f", touch_point.x);
    NSLog(@"Y Location: %f",touch_point.y);

    CGPoint point = [touch locationInView:self.view];

    croperImgvw.image = [self cropImage:[imgVw image] :point];


}

【问题讨论】:

    标签: ios objective-c uiimage crop xcode6.1


    【解决方案1】:

    您似乎正在传递 视图 中的坐标,以便裁剪成 图像。图像视图和它的图像很少有相同的尺寸,尤其是当您从照片中挑选图像时。

    在发送要裁剪的图像之前,尝试将第一个视图渲染为图像。您可以通过向 UIView 添加一个类别来做到这一点,如下所示:

    @implementation UIView (Image)
    
    - (UIImage *)image {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    @end
    

    编辑:或者如果您只是想让它在没有类别的情况下工作,请将此方法添加到您的代码中:

    - (UIImage *)imageOfView:(UIImageView *)imageView {
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
        [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    然后修改你现有的代码为:

    croperImgvw.image = [self cropImage:[self imageOfView:imgVw] :point];
    

    【讨论】:

    • 感谢您的回复,我是 iOS 开发的新手,您能帮我看看我在哪里或如何添加您的代码或想法。
    • 我已经编辑了我的答案,因此您可以直接添加代码。您也无需担心针对不同屏幕尺寸的缩放,因为 UIGraphicsBeginImageContextWithOptions 调用中的 0.0 参数会自动为设备屏幕选择正确的缩放因子。
    • 我已经尝试了这些代码,它还分配了一个距离源图像最近的代码,例如,如果我在图像的“眼睛”附近单击意味着它在“颈部”或“嘴巴”附近裁剪图像.我从早上开始就一直在努力解决这个问题,但仍然无法解决。
    • 哦,我刚刚发现了一些别的东西——你可能希望CGPoint point = [touch locationInView:self.view]; 改为CGPoint point = [touch locationInView:imgVw.view];
    • @Mohanraj,这些答案对你有用吗?如果是,请选择一个作为正确答案。 (附:我将此添加到我自己的答案中,因为这是一个新帐户,我还没有足够的代表来评论这个问题)。
    【解决方案2】:

    增加订单的答案 分辨率不同,加个scale参数就好了。

    TakeSnapshot.h

    #import <Foundation/Foundation.h>
    
    @interface TakeSnapshot : NSObject
    
    
    +(UIImage *)takeSnapshotFromScreenWithSize:(UIView *)view Area:(CGPoint)screenPoint; 
    
    @end
    

    TakeSnapshot.m

    #import "TakeSnapshot.h"
    
    @implementation TakeSnapshot
    +(UIImage *)takeSnapshotFromScreenWithSize:(UIView *)view Area:(CGPoint)screenPoint{
        {
            if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
                UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
            }
            else
                UIGraphicsBeginImageContext(view.bounds.size);
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            //    
            if([[UIScreen mainScreen] respondsToSelector:@selector(scale)])  UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.width),NO,[UIScreen mainScreen].scale);
            else
                UIGraphicsBeginImageContext(view.bounds.size);
            [screenImage drawAtPoint:screenPoint];
            UIImage *shareImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            return shareImage;
    
        }
    
    }
    @end
    

    【讨论】:

      【解决方案3】:

      据我了解,您传递给 cropImage:image: 方法的 point 参数来自 UIImageView 坐标系 - CGImageCreateWithImageInRect 中的 rect 参数必须来自 UIImage ,而不是来自 UIImageView。

      以下是您如何解决此问题的几个答案:

      https://stackoverflow.com/a/10865552/4495995

      https://stackoverflow.com/a/21693491/4495995

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-26
        • 1970-01-01
        相关资源
        最近更新 更多