iOS:setImageWithURL请求而不裁剪图像

时间:2013-12-09 14:15:56

标签: ios objective-c image uiimageview uiimage

我有一个包含UIImageView的viewController。我想使用像这样的方面填充来呈现它:

enter image description here

我还希望用户能够点击图像以获得具有正确宽高比的全屏图像。但是,似乎setImageWithURLRequest方法在成功块中返回之前改变了UIImage本身。这是我用来显示图像的代码:

    [coverImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://XXXXXXXXXX/index.aspx?isbn=%@/lc.gif&client=XXXXXXX",coverImage.isbn]]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

        [coverImageSpinner stopAnimating];
        [coverImageSpinner setHidden:YES];
        if (image.size.height>1) {
            CGImageRef newCgIm = CGImageCreateCopy(image.CGImage);
            UIImage *newImage = [UIImage imageWithCGImage:newCgIm scale:image.scale orientation:image.imageOrientation];
            originalImage=newImage;
            if (detailPaneFocused) {
                [UIView transitionWithView:self.view duration:.33f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                    coverImage.image=image;
                } completion:^(BOOL finished) {

                }];
            }else{
                    coverImage.image=image;
            }


        }else{
            coverImage.image=[UIImage imageNamed:@"book_fixed"];
        }

所以我试图在为方面填充进行裁剪之前复制原始图像,但这不起作用:

enter image description here

有没有办法在根据UIImageView的内容模式裁剪之前抓取实际下载的图像?

谢谢!

编辑:我尝试了下面的建议,但奇怪的是它仍然没有用。这是下载图像的代码:

    NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.syndetics.com/index.aspx?isbn=%@/lc.gif&client=260-421-1200",coverImage.isbn]];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *downloadedImage = [UIImage imageWithData:imageData];

    [coverImageSpinner stopAnimating];
    [coverImageSpinner setHidden:YES];
    if (downloadedImage.size.height>1) {
        CGImageRef newCgIm = CGImageCreateCopy(downloadedImage.CGImage);
        originalImage = [UIImage imageWithCGImage:newCgIm scale:downloadedImage.scale orientation:downloadedImage.imageOrientation];
        if (detailPaneFocused) {
            [UIView transitionWithView:self.view duration:.33f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                coverImage.image=downloadedImage;
            } completion:^(BOOL finished) {

            }];
        }else{
            coverImage.image=downloadedImage;
        }


    }else{
        coverImage.image=[UIImage imageNamed:@"book_fixed"];
    }

这是点击图像时调用的方法。我仍然得到裁剪的图像。它是否与我正在设置的边界有关,因为它是横向的?

if(originalImage!=nil){
    UIViewController *modalCon = [[UIViewController alloc] init];

    modalCon.view.backgroundColor=[UIColor blackColor];
    modalCon.view.userInteractionEnabled=YES;



    UIImageView *imageView = [[UIImageView alloc] initWithFrame:modalCon.view.bounds];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];

    [imageView setImage:originalImage];

    [imageView setCenter:self.view.center];
    [modalCon.view addSubview:imageView];


    UITapGestureRecognizer *modalTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissModalView)];
    [modalCon.view addGestureRecognizer:modalTap];
    modalCon.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:modalCon animated:YES completion:nil];
    NSLog(@"%f",modalCon.view.frame.origin.y);
    NSLog(@"%f",modalCon.view.frame.origin.x);
}

2 个答案:

答案 0 :(得分:0)

在使用setImage方法之前,你总是可以获取原始图像:

NSURL *imageURL = [NSURL URLWithString:@"Your URL"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];

答案 1 :(得分:0)

事实证明问题是:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:modalCon.view.bounds];

即使应用程序在横向模式下被锁定,这也为UIImageView提供了纵向尺寸(高度比宽度更大)。通过简单地交换UIImageView的宽度和高度,我得到了正确的外观。