gpt4 book ai didi

objective-c - 具有无效上下文的 CoreGraphics drawAtPoint

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:16 25 4
gpt4 key购买 nike

我对核心图形有点陌生,我一直出错,当我合并它们时第二张图片没有显示:

- (UIImage*)imageByCombiningImage:(UIImage*)gfirstImage withImage:(UIImage*)gsecondImage atPositionX:(int)xPosition withPositionY:(int)yPosition{

UIImage *firstImage = nil;
UIImage *secondImage = nil;

firstImage = gfirstImage;
secondImage = gsecondImage;

// int ratio = secondImage.size.height/secondImage.size.width;
// int newWidth = firstImage.size.width/3;
// int newHeight = (firstImage.size.height/3)*ratio;
CGSize scaledSize = CGSizeMake(firstImage.size.width, firstImage.size.height);
CGSize badgeScaledSize = scaledSize;
if(firstImage.size.width > 500){
scaledSize = CGSizeMake(firstImage.size.width/4, firstImage.size.height/3);
}

if(firstImage.size.width < firstImage.size.height){
badgeScaledSize = CGSizeMake((firstImage.size.width/4)*prevPinchScale, (firstImage.size.height/4)*prevPinchScale);
}

if(firstImage.size.width > firstImage.size.height){
badgeScaledSize = CGSizeMake((firstImage.size.width/4)*prevPinchScale, (firstImage.size.height/4)*prevPinchScale);
}



secondImage = [secondImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:badgeScaledSize interpolationQuality:0.5];

CGSize newImageSize = CGSizeMake(firstImage.size.width, firstImage.size.height);

UIGraphicsBeginImageContext(newImageSize);

NSLog(@"first image size width: %f, size height: %f", firstImage.size.width, firstImage.size.height);
NSLog(@"first image size width: %f, size height: %f", newImageSize.width, newImageSize.height);

[firstImage drawAtPoint:CGPointMake(0,
0)];


[secondImage drawAtPoint:CGPointMake(xPosition,
yPosition)];

UIImage *image = nil;
image = UIGraphicsGetImageFromCurrentImageContext();

NSLog(@"got image of width: %f and height: %f", image.size.width, image.size.height);

UIGraphicsEndImageContext();


return image;
}

错误如下:

 <Error>: CGContextConcatCTM: invalid context 0x0
<Error>: CGContextSetInterpolationQuality: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGBitmapContextCreateImage: invalid context 0x0

最佳答案

这些错误实际上不是从您的代码中抛出的,而是在我假设您正在使用的 UIImage+Resize.m 中抛出的。如果您删除该行,您的代码运行正常

secondImage = [secondImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:badgeScaledSize interpolationQuality:0.5];

如果您查看该方法,您会发现它调用了另一个方法,该方法进行了您从中得到错误的四个调用。

- (UIImage *)resizedImage:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = self.CGImage;

// Build a context that's the same dimensions as the new size
CGContextRef bitmap = CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(imageRef),
0,
CGImageGetColorSpace(imageRef),
CGImageGetBitmapInfo(imageRef));

// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);

// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);

return newImage;
}

似乎值位图无效。我会检查以确保在 CGBitmapContextCreate 调用中,您传递的是真实值而不是创建大小为 0x0 的上下文。

关于objective-c - 具有无效上下文的 CoreGraphics drawAtPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17595087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com