1

3 つの画像 (.png 形式) があります。3 つの画像を結合し、画像の 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*)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 に答える
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 に答える