【问题标题】:How to resize CIImage?如何调整 CIImage 的大小?
【发布时间】:2012-02-25 07:42:28
【问题描述】:

我需要在一个“for”中调整更多图像的大小,但是如果我使用 UIGraphicsGetImageFromCurrentImageContext,我没有足够的内存来存储更多图像,因为它会保持自动释放,并且在“for”终止时释放图像。我需要另一种调整大小的方法。有任何想法吗。谢谢

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
CGSize targetSize = newSize;      
CGSize imageSize = image.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;

[image drawInRect:thumbnailRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//pop the context to get back to the default
UIGraphicsEndImageContext();

return newImage;
}


-(void)preparePhotosForResolution:(CGSize)resolution {
NSLog(@"Resolution : %f,%f",resolution.width,resolution.height);
NSFileManager *fm = [NSFileManager defaultManager];
NSString *tmpPath = [projectPath stringByAppendingPathComponent:@"temp"];
[fm removeItemAtPath:tmpPath error:nil];
[fm createDirectoryAtPath:tmpPath withIntermediateDirectories:YES attributes:nil error:nil];
for (int i = 0; i < [sortArray count]; i++) {
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[projectPath stringByAppendingPathComponent:[sortArray objectAtIndex:i]]];
    UIImage *newImage = [self imageWithImage:image scaledToSize:resolution];
    [image release];
    NSData *imgData = UIImagePNGRepresentation(newImage);
    [fm createFileAtPath:[tmpPath stringByAppendingPathComponent:[sortArray objectAtIndex:i]] contents:imgData attributes:nil];
}
}

【问题讨论】:

  • 根据谷歌翻译,安德鲁的评论是“白,你秃了,所以你不知道问题的答案”。与此同时,Igor,你能展示一下你用来调整图像大小的代码吗?这可能会帮助我或其他任何人想出您的图像大小调整问题的答案。
  • 准确的翻译是“嘿,你是皮肤,所以你不知道问题的答案”;)
  • @IgorBidiniuc 请看这里:stackoverflow.com/questions/5860215/…
  • 这很好,但这是缩略图...

标签: iphone objective-c uiimage core-graphics core-image


【解决方案1】:
    CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
    [filter setValue:newImage forKey:@"inputImage"];
    [filter setValue:@(scale) forKey:@"inputScale"];
    [filter setValue:@1.0 forKey:@"inputAspectRatio"];
    newImage = filter.outputImage;

【讨论】:

猜你喜欢
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多