【问题标题】:How to Multiple UIImages cropping and saving in a single loop如何在单个循环中裁剪和保存多个 UIImages
【发布时间】:2014-07-15 23:18:10
【问题描述】:

在我的项目中最多使用 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;

}

【问题讨论】:

  • 您在使用 ARC 吗?如果是这样,那么你不应该调用释放。如果没有,那么你有很多内存泄漏
  • 这不是真的,这些图像在自动释放池中,直到下一个线程周期才会被耗尽。

标签: ios objective-c xcode ios7 uiimage


【解决方案1】:

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

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

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

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

[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];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多