gpt4 book ai didi

ios - 如何在单个循环中裁剪和保存多个 UIImages

转载 作者:行者123 更新时间:2023-11-29 02:56:16 28 4
gpt4 key购买 nike

在我的项目中最多使用 60 张图像,我的功能之一是需要以给定的比例自动裁剪所有 60 张图像。我在这个实现中使用了 for 循环。for 循环内部包含裁剪和保存图像。它被实现了。但由于内存压力,我的应用程序在设备中崩溃。请帮助我

for (int ref=0; ref<[_selectedPhotosCollectionthumb count];ref++)
{
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,biManager.screenSize.height/2,biManager.screenSize.width,biManager.screenSize.height/2)];
[scrollView setDelegate:self];
[scrollView setBackgroundColor:[UIColor clearColor]];
[self addSubview:scrollView];
// scrollView.backgroundColor=[UIColor blueColor];
scrollView.userInteractionEnabled=YES;
scrollView.scrollEnabled=YES;
scrollView.tag=ref;
scrollView.hidden=YES;
[_scrollViews addObject:scrollView];


NSLog(@"%i",[_selectedPhotosCollection count]);

NSMutableArray *arrayCell=[_productCollectionsDict valueForKey:[_selectedPhotosCollection objectAtIndex:ref]];
int heightV=0;

for (int cellIndex=0;cellIndex<[arrayCell count];cellIndex++)
{

PrintCellView *cellObj=[arrayCell objectAtIndex:cellIndex];
if(cellObj.pCount>0)
{
PrintEditCellView *cell;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
cell=[[PrintEditCellView alloc] initWithFrame:CGRectMake(0,heightV*100,biManager.screenSize.width,100)];
scrollView.contentSize=CGSizeMake(0,heightV*100+100);
cell.delegate=self;

}
else
{

cell=[[PrintEditCellView alloc] initWithFrame:CGRectMake(0,heightV*50,biManager.screenSize.width,50)];
scrollView.contentSize=CGSizeMake(0,heightV*50+50);
cell.delegate=self;

}



NSDate *now = [NSDate date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(@"The Current Time is %@",[dateFormatter stringFromDate:now]);

// NSData *imageData=UIImageJPEGRepresentation(Thumbimage,1.0);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString* path = [documentsDirectory stringByAppendingPathComponent:[_selectedPhotosCollection objectAtIndex:ref]];

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:path];

UIImage *image1=[[UIImage alloc]initWithData:data];

cell.productName.text=cellObj.productName.text;

UIImage * image=[self imageByCropping:image1 CropRatio:cell.productName.text];



NSLog(@"CROPPPP");
NSData *imageData= [[NSData alloc] initWithData:UIImageJPEGRepresentation(image,1.0)];

//
NSString* path1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Prydex%i%@.jpg",cellIndex,[dateFormatter stringFromDate:now]]];
NSLog(@"pthhh:%@",path1);

[imageData writeToFile:path1 atomically:YES];
cell.editedImageURL=path1;

NSLog(@"%@,%i",cellObj.productName.text,cellObj.pCount);
[scrollView addSubview:cell];
[cell release];
heightV=heightV+1;
[dateFormatter release];

[image1 release];
// [imageData release];


// [image release];
}

}

//NSLog(@"Scroll Count %i",[_scrollViews count]);
for (UIScrollView *scrollView in _scrollViews)
{
if (scrollView.tag==0)
{
scrollView.hidden=NO;
}
else
{

scrollView.hidden=YES;
}
}
[SVProgressHUD dismiss];

}

裁剪代码

- (UIImage *)imageByCropping:(UIImage *)image CropRatio:(NSString*)ratio

{ CGSize大小;

NSArray *array=[ratio componentsSeparatedByString:@"*"];

NSString *productWidth=[array objectAtIndex:0];

NSString *productHeight=[array objectAtIndex:1];

NSLog(@"SIZE:%@,%@",productWidth,productHeight);
NSLog(@"SIZE:%f,%f",image.size.width,image.size.height);

if (image.size.width/[productWidth intValue]>=230)
{
if (image.size.height/[productHeight intValue]>=230) {

size=CGSizeMake([productWidth intValue]*230,[productHeight intValue]*230);
NSLog(@"SIZE Inner:%i,%i",[productWidth intValue],[productHeight intValue]);


}
else if(image.size.width/[productWidth intValue]>=100)
{

if (image.size.height/[productHeight intValue]>=100)
{

size=CGSizeMake([productWidth intValue]*100,[productHeight intValue]*100);
NSLog(@"SIZE outer:%i,%i",[productWidth intValue],[productHeight intValue] );

}
}


}
else if(image.size.width/[productWidth intValue]>=100)
{

if (image.size.height/[productHeight intValue]>=100)
{

size=CGSizeMake([productWidth intValue]*100,[productHeight intValue]*100);
NSLog(@"SIZE outer:%i,%i",[productWidth intValue],[productHeight intValue] );

}
}
NSLog(@"crop---->%@",NSStringFromCGSize(size));

double x = (image.size.width - size.width) / 2.0;
double y = (image.size.height - size.height) / 2.0;

CGRect cropRect = CGRectMake(x, y, size.width, size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return cropped;

}

最佳答案

解决方案可能是使用递归:

创建一个方法,获取需要处理的图像数组。在方法内部检查数组计数是否为零。

如果它是空的,你应该返回,可能会做一些回调来通知应用程序你的图像处理已经完成。

如果数组不为空,则从数组中取出第一张图像,进行所有处理,然后从数组中删除第一个对象并调用相同的方法,新数组缺少该元素。电话应该是这样的

[self performSelector:@selector(methodName:) withObject:imageArray];

全部应该看起来像这样:

- (void)processImages:(NSArray *)images {
if(images.count < 1) {
[self performSelectorOnMainThread:@selector(imageProcessingDone) withObject:nil waitUntilDone:NO];
}
else {
UIImage *toProcess = images[0];
NSMutableArray *newArray = [images mutableCopy];
[newArray removeObjectAtIndex:0];

//do the processing

[self performSelector:@selector(processImages:) withObject:newArray];
}
}

关于ios - 如何在单个循环中裁剪和保存多个 UIImages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23882722/

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