iphone - 如何调整CIImage的大小?

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

我需要在一个“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];
}
}

最佳答案

    CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
    [filter setValue:newImage forKey:@"inputImage"];
    [filter setValue:@(scale) forKey:@"inputScale"];
    [filter setValue:@1.0 forKey:@"inputAspectRatio"];
    newImage = filter.outputImage;

关于iphone - 如何调整CIImage的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9115949/

相关文章:

objective-c - 从不兼容类型 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' 分配给 'CameraVIewController*' '

objective-c - 如何停止 NSTimer

iphone - 设备选择了错误的视网膜图像

ios - 将 UIImage 转换为 NSData 并在 Swift 中转换回 UIImage?

ios - UITableView 'hidden' 部分导致每次拉动刷新时分配更多内存

ios - *** 错误 *** CGImageSource 创建时数据大小为 : 22467 - current size is only: 6139

iphone - 如何实现每天只显示一个组件的 UIDatepicker?

iphone - MKAnnotation协议(protocol)的 "coordinate"属性

iphone - iOS Advanced Audio API 解压格式

iphone - 在 webview 上标记