在iPhone sdk中resize后获取图像的大小

我有图像视图,我正在显示从图书馆中select的图像。 要显示我使用以下方法重新缩放图片的高质量图片。 我得到的图像质量是完美的,但我需要根据新创build的图像的大小设置imageView框架。 但是当我使用newImage.size.width它给我的原始图像视图的宽度。 请帮助我根据显示的图像大小设置图像视图框架。 提前致谢

-(UIImage *)scaleImage:(UIImage *)img toRectSize:(CGRect)screenRect { UIGraphicsBeginImageContext(screenRect.size); float hfactor = img.size.width / screenRect.size.width; float vfactor = img.size.height / screenRect.size.height; float factor = MAX(hfactor, vfactor); float newWidth = img.size.width / factor; float newHeight = img.size.height / factor; float leftOffset = (screenRect.size.width - newWidth) / 2; float topOffset = (screenRect.size.height - newHeight) / 2; CGRect newRect = CGRectMake(leftOffset, topOffset, newWidth, newHeight); [img drawInRect:newRect blendMode:kCGBlendModePlusDarker alpha:1]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

试试这个代码,我用来调整图像大小,你也会得到新的框架。比例似乎是固定的,但你可以根据你的要求改变它。

 -(UIImage*)ImageResize:(UIImage*)image { if(image==NULL) { return NULL; } else { float actualHeight = image.size.height; float actualWidth = image.size.width; float imgRatio = actualWidth/actualHeight; float maxRatio = 130.0/160.0; if(imgRatio!=maxRatio) { if(imgRatio < maxRatio) { imgRatio = 160.0 / actualHeight; actualWidth = imgRatio * actualWidth; actualHeight = 160.0; } else { imgRatio = 130.0 / actualWidth; actualHeight = imgRatio * actualHeight; actualWidth = 130.0; } } CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); UIGraphicsBeginImageContext(rect.size); [image drawInRect:rect]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } } 

下面的代码可以用于您可以传递的特定图像大小。

  -(UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)wantSize { UIImage * targetImage; if (nil == image) { targetImage = nil; }else{ CGSize size = image.size; CGRect rect; if (wantSize.width/wantSize.height > size.width/size.height) { rect.size.width = wantSize.height*size.width/size.height; rect.size.height = wantSize.height; rect.origin.x = (wantSize.width - rect.size.width)/2; rect.origin.y = 0; } else{ rect.size.width = wantSize.width; rect.size.height = wantSize.width*size.height/size.width; rect.origin.x = 0; rect.origin.y = (wantSize.height - rect.size.height)/2; } UIGraphicsBeginImageContext(wantSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); UIRectFill(CGRectMake(0, 0, wantSize.width, wantSize.height));//clear background [image drawInRect:rect]; targetImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } return targetImage; } 

尝试这个,

 resizedImage = [self imageWithImage:originalImage scaledToSize:CGSizeMake(45,45)]; self.imageView.image = resizedImage; - (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

你在使用Autolayout吗? 如果是的话,你必须改变你的图像视图的宽度和高度限制,而不是框架(你可以像创build其他控件一样创build它们的sockets)。

对于自动布局,你可以改变viewDidAppear方法中的框架(在viewWillAppear或viewWillLoad它可能无法正常工作。