裁剪大尺寸图像并在imageview的框架中设置

时间:2014-07-28 12:46:51

标签: ios uiimageview uiimage cgrect

这个问题需要很多时间,但我的问题却完全不同。

我从 Json (网络服务)获取图片。它" http:/address/upload/book_icon/4353988696.jpg"。

我需要在imageview中显示 85 * 130尺寸的图像。但大多数图像都有更大的尺寸。

例如:如果图像的大小为1000 * 650,那么我如何在85 * 130中设置此图像而不下载它。

为了更好地显示我正在使用的图像" SDWebImageManagerDelegate"但需要以速度裁剪图像并在imageview中显示并下载。

我到目前为止......

NSString *stringURL = [NSString stringWithFormat:@"%@",[imageURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 85,130)];
        [imgView setBackgroundColor:[UIColor whiteColor]];
        imgView.tag = i;

但由于裁剪图像而没有在线下用于获取图像...

        [imgView setImageWithURL:[NSURL URLWithString:stringURL] placeholderImage:[UIImage imageNamed:@"no_image.png"]];  

并用它来获取图像......

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringURL]];
        UIImage *image123temp = [UIImage imageWithData:data];
UIImage *objimgtemp = [self imageCrop:image123temp];

        imgView.image = objimgtemp; 

使用此方法裁剪图像

-(UIImage*)imageCrop:(UIImage*)original
{
UIImage *ret = nil;

// This calculates the crop area.

float originalWidth  = original.size.width;
float originalHeight = original.size.height;

float edge = fminf(originalWidth, originalHeight);

float posX = (originalWidth   - edge) / 2.0f;
float posY = (originalHeight  - edge) / 2.0f;


CGRect cropSquare = CGRectMake(posX, posY,
                               edge, edge);


// This performs the image cropping.

CGImageRef imageRef = CGImageCreateWithImageInRect([original CGImage], cropSquare);

ret = [UIImage imageWithCGImage:imageRef
                          scale:original.scale
                    orientation:original.imageOrientation];

CGImageRelease(imageRef);

NSLog(@"ret.......... %f == %f",ret.size.width,ret.size.height);

return ret;
}

如何快速获得裁剪图像?

任何链接,教程,建议,代码都会有很大的帮助......

1 个答案:

答案 0 :(得分:0)

始终按原样下载位于远程URL的图像。在远程服务器上创建图像的缩略图版本的任何内容,或者在客户端请求时自动调整图像或自动切换图像 是服务器端进程而不是客户端进程。

现在你要做的是创建一个简单的服务器端webrequest(用PHP或其他语言),它返回一个 当然尺寸较小的裁剪图像。 然后你会在客户端做这样的事情:

NSString *stringURL = [NSString stringWithFormat:@"http://www.example.com/getCroppedImage?width=%f&height=%f&imagename=%@", width, height, someImageName];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringURL]];
相关问题