1

我有三个图像(.png 格式)。我组合了三个图像并使用 drawInRect() 为图像创建了一个对话气泡,并指定了绘制每个图像的矩形。

我使用 resizableImageWithCapInsets:UIEdgeInsetsMake 为每个图像创建了可调整大小的图像。

我的原始图像很大。因此,当我添加仅包含几个字符的文本时,绘制的语音气泡对于文本来说看起来更大。

谁能告诉我如何缩小图像以使其适当地围绕文本。

我的语音气泡有一个顶部图像,其原始高度为 17 像素,因此如果我使用 drawInRect() 和低于该高度的矩形,图像的一部分会被裁剪,底部图像也是如此。

任何帮助表示赞赏。谢谢你。

4

4 回答 4

1

我们需要根据需要调整图像大小,示例代码片段:

    CGSize imageSize = CGSizeMake(320,480); // give any desired size you want

    UIGraphicsBeginImageContext(itemSize);//Creates a bitmap-based graphics context

    CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);//modify

    [myimage drawInRect:imageRect];


    myimage = UIGraphicsGetImageFromCurrentImageContext();  //returns new image

    UIGraphicsEndImageContext();

希望这可以帮助。

于 2012-12-13T05:40:36.460 回答
0

试试这个方法来调整图像大小

-(UIImage *)resizeImage:(UIImage *)image width:(int)wdth height:(int)hght{
    int w = image.size.width;
    int h = image.size.height;
    CGImageRef imageRef = [image CGImage];
    int width, height;
    int destWidth = wdth;
    int destHeight = hght;
    if(w > h){
        width = destWidth;
        height = h*destWidth/w;
    }
    else {
        height = destHeight;
        width = w*destHeight/h;
    }
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap;
    bitmap = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);

    if (image.imageOrientation == UIImageOrientationLeft) {

        CGContextRotateCTM (bitmap, M_PI/2);
        CGContextTranslateCTM (bitmap, 0, -height);

    } else if (image.imageOrientation == UIImageOrientationRight) {

        CGContextRotateCTM (bitmap, -M_PI/2);
        CGContextTranslateCTM (bitmap, -width, 0);

    }
    else if (image.imageOrientation == UIImageOrientationUp) {

    } else if (image.imageOrientation == UIImageOrientationDown) {

        CGContextTranslateCTM (bitmap, width,height);
        CGContextRotateCTM (bitmap, -M_PI);
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];
    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;

}
于 2012-12-21T05:05:20.513 回答
0
 - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;        
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
{
    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    if (widthFactor > heightFactor) 
        scaleFactor = widthFactor; // scale to fit height
    else
        scaleFactor = heightFactor; // scale to fit width.

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // center the image
    if (widthFactor > heightFactor)
    {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    }
    else 
        if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
}       

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) 
NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
于 2012-12-13T05:33:09.910 回答
0

试试这个方法:

-(UIImage *)imageWithImage:(UIImage *)imageToCompress scaledToSize:(CGSize)newSize {

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [imageToCompress drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
于 2012-12-13T05:52:53.623 回答