【问题标题】:Crop image before show in a UIImageView在 UIImageView 中显示之前裁剪图像
【发布时间】:2013-07-22 10:12:48
【问题描述】:

我是我的应用,我需要裁剪和从互联网下载的图像。我使用这种方法下载图像:

- (void) loadImageFromWeb {
    NSURL* url = [NSURL URLWithString:self.imageURL];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   UIImage* image = [[UIImage alloc] initWithData:data];
                                   [self.imageViewEpisode setImage:image];
                               }

                           }];
}

如何裁剪它?

【问题讨论】:

    标签: ios uiimageview crop


    【解决方案1】:

    定义一个矩形,这个矩形将是您图像的裁剪区域。

    CGRect croprect  = CGRectMake(x,y,width, height);
    
    CGImageRef subImage = CGImageCreateWithImageInRect (yourimage,croprect);
    

    这里我们使用 CoreGraphics 从您的图像创建子图像。

    Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
    
    CGImageRef CGImageCreateWithImageInRect (
       CGImageRef image,
       CGRect rect
    );
    
    Parameters
    
    image
    
        The image to extract the subimage from. 
    rect
    
        A rectangle whose coordinates specify the area to create an image from.
    
    Return Value
    
    A CGImage object that specifies a subimage of the image. If the rect parameter defines an area that is not in the image, returns NULL.
    

    最后生成图片,

    UIImage *newImage = [UIImage imageWithCGImage:subImage];

    【讨论】:

    • 我也会尝试这个解决方案。谢谢!
    【解决方案2】:

    multiple libraries 可以为您做到这一点。您可以选择一个并使用它,或者研究一对并了解它是如何完成的。

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多