【问题标题】:Objective-C iOS Crop Image Using Zoom?使用缩放的 Objective-C iOS 裁剪图像?
【发布时间】:2012-11-26 21:02:16
【问题描述】:

我正在尝试使用此代码来允许用户在我的应用中缩放和裁剪他们的图像: https://github.com/iosdeveloper/ImageCropper/tree/master/Classes/ImageCropper

代码sn-p

float zoomScale = 1.0 / [scrollView zoomScale];

CGRect rect;
rect.origin.x = [scrollView contentOffset].x * zoomScale;
rect.origin.y = [scrollView contentOffset].y * zoomScale;
rect.size.width = [scrollView bounds].size.width * zoomScale;
rect.size.height = [scrollView bounds].size.height * zoomScale;

CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);

UIImage *cropped = [UIImage imageWithCGImage:cr];

CGImageRelease(cr);

[delegate imageCropper:self didFinishCroppingWithImage:cropped];

但它没有考虑旋转的图像(我猜这是 iPhone 图像上的某种元数据)。我不想让用户画一个矩形来裁剪,因为我更喜欢这个界面。

谁能告诉我如何让它不旋转我的图像?

如果有帮助,我正在 iOS 5 和 6 中构建。

[编辑] 此外,这在 Game Center 应用程序中很好地完成,用于上传您的个人资料图片。有人知道我在哪里可以找到它的代码吗?

【问题讨论】:

    标签: ios zooming crop


    【解决方案1】:

    我在这里找到了一个帮助我做我想做的事情的答案 iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

    感兴趣的人的代码片段

    float zoomScale = 1.0 / [scrollView zoomScale];
    
    CGRect rect;
    NSLog(@"contentOffset is :%f,%f",[scrollView contentOffset].x,[scrollView contentOffset].y);
    rect.origin.x = fabsf([scrollView contentOffset].x * zoomScale);
    rect.origin.y = fabsf([scrollView contentOffset].y * zoomScale);
    rect.size.width = fabsf([scrollView bounds].size.width * zoomScale);
    rect.size.height = fabsf([scrollView bounds].size.height * zoomScale);
    
    UIGraphicsBeginImageContextWithOptions( CGSizeMake( rect.size.width,
                                                       rect.size.height),
                                           NO,
                                           0.);
    [[imageView image] drawAtPoint:CGPointMake( -rect.origin.x, -rect.origin.y)
                blendMode:kCGBlendModeCopy
                    alpha:1.];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    【讨论】:

    • 现在这会导致使用大量内存.. 有更好的解决方案吗?
    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 2011-04-23
    • 2016-08-29
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多