Reputation: 1560
I need to resize more image in one "for" but if I use UIGraphicsGetImageFromCurrentImageContext, I don't have enough memory for more images, because it stay on autorelease and images released when "for" is terminated. I need another method for resize. Any ideas. Thanks
-(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];
}
}
Upvotes: 2
Views: 7100
Reputation: 11
With above objective-c code in swift.
inputImage = CIImage
inputScale = targetSize.width / originalSize.width
inputAspectRatio = targetSize.width / (originalSize.width * resizeRatio)
let resizeFilter = CIFilter(name: "CILanczosScaleTransform")
resizeFilter?.setValue(inputImage, forKey: "inputImage")
resizeFilter?.setValue(inputScale, forKey: "inputScale")
resizeFilter?.setValue(inputAspectRatio, forKey: "inputAspectRatio")
let outputImage = resizeFilter?.outputImage
Upvotes: -1
Reputation: 251
CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
[filter setValue:newImage forKey:@"inputImage"];
[filter setValue:@(scale) forKey:@"inputScale"];
[filter setValue:@1.0 forKey:@"inputAspectRatio"];
newImage = filter.outputImage;
Upvotes: 3