0

touchesEnded描画アプリケーションで次のコードを呼び出しています。ただし、これが呼び出されるたびに、メモリ使用量が約 2MB 増加します。最終的には約 90 ~ 100 MB になる可能性があり、ユーザーがすばやく描画するとアプリがクラッシュします。

ここで何が間違っていますか?最小限のメモリ使用量でこのコードを高速化するにはどうすればよいですか?

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    if(_pagessavedbg.count > selectedIndex)
        self.topimage = [[UIImage alloc] initWithCGImage:[[_pagessavedbg objectAtIndex:selectedIndex] CGImage]];

    else
        self.topimage = [[UIImage alloc] initWithCGImage:[[UIImage imageNamed:@"BlankImage.png"] CGImage]];

    UIImage *bottomimage = [notification.userInfo objectForKey:@"Image"];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSError *error2 = nil;

    NSURL *documentsURL = [[NSFileManager defaultManager]
                           URLForDirectory:NSDocumentDirectory
                           inDomain:NSUserDomainMask
                           appropriateForURL:nil
                           create:YES
                           error:nil];

    CGSize size = CGSizeMake(874, 732);
    UIGraphicsBeginImageContextWithOptions(size, NO, 1);
    [self.topimage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    [bottomimage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *latest = UIGraphicsGetImageFromCurrentImageContext();
    [activeView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *latest2 = UIGraphicsGetImageFromCurrentImageContext();
    CGContextFlush(UIGraphicsGetCurrentContext());
    UIGraphicsEndImageContext();

    if(_pages.count > selectedIndex)
    {
    if([_pages objectAtIndex:selectedIndex])
    [_pages replaceObjectAtIndex:selectedIndex withObject:latest];
    }

    if(_pages.count > selectedIndex)
    {
    if([_pagessavedbg objectAtIndex:selectedIndex])
    [_pagessavedbg replaceObjectAtIndex:selectedIndex withObject:latest2];

    else
    [_pagessavedbg insertObject:latest2 atIndex:selectedIndex];
    }

    NSData *data = UIImageJPEGRepresentation(latest, 1.0);

    NSError *error = nil;

    NSDictionary *fileObjectMap = @{
    @"ImageData" : data,
    };

    for (NSString *filename in fileObjectMap)
    {
        NSData       *data = [NSPropertyListSerialization
                              dataWithPropertyList:fileObjectMap[filename]
                              format:NSPropertyListXMLFormat_v1_0
                              options:0
                              error:&error];

        if (!data) {
            NSLog(@"Failed to serialize array for filename %@ (contents %@) with error %@", filename, fileObjectMap[filename], error);
            return;
        }

        NSURL        *fileURL = [[documentsURL URLByAppendingPathComponent:filename]
                                 URLByAppendingPathExtension:@"txt"];

        if (![data writeToURL:fileURL options:NSDataWritingAtomic error:&error]) {
            NSLog(@"Failed to write data to file URL %@ for filename %@ (data %@) with error %@", fileURL, filename, data, error);
            return;
        }
    }

    NSString *indexnotext = [defaults objectForKey:@"IndexNumber"];
    myInt = [indexnotext intValue];

    dispatch_async(dispatch_get_main_queue(), ^{
    [self.sidebar reloadData];
    [self.sidebar scrollRowAtIndexToVisible:_sidebar.selectedIndex];
    });    

    if (!documentsURL) {
        NSLog(@"Failed to get documents URL: %@", error2);
        return;
    }

    NSMutableArray *imageDatas = [NSMutableArray arrayWithCapacity:self.pages.count];

    for (UIImage *image in [_pages copy])
    {

        if(!image)
        break;

        else
        [imageDatas addObject:UIImageJPEGRepresentation(image, 1.0)];

    }

    NSMutableArray *imageDatas2 = [NSMutableArray arrayWithCapacity:self.pagessavedbg.count];

    for (UIImage *image2 in [self.pagessavedbg copy])
    {
        if(!image2)
        break;

        else
        [imageDatas2 addObject:UIImageJPEGRepresentation(image2, 1.0)];

    }

    NSDictionary *fileObjectMap2 = @{
    [NSString stringWithFormat:@"SavedPages%i", [_indexnumber intValue]] : imageDatas,
    [NSString stringWithFormat:@"SavedPagesBG%i", [_indexnumber intValue]] : imageDatas2,
    };

    for (NSString *filename2 in fileObjectMap2)
    {
        self.data2 = [NSPropertyListSerialization
                              dataWithPropertyList:fileObjectMap2[filename2]
                              format:NSPropertyListXMLFormat_v1_0
                              options:0
                              error:&error2];

        if (!self.data2) {
            NSLog(@"Failed to serialize array for filename %@ (contents %@) with error %@", filename2, fileObjectMap2[filename2], error2);
            return;
        }

        NSURL        *fileURL2 = [[documentsURL URLByAppendingPathComponent:filename2]
                                 URLByAppendingPathExtension:@"txt"];

        if (![self.data2 writeToURL:fileURL2 options:NSDataWritingAtomic error:&error2]) {
            NSLog(@"Failed to write data to file URL %@ for filename %@ (data %@) with error %@", fileURL2, filename2, self.data2, error2);
            return;
        }

    }

});
4

1 に答える 1

-1

XCODE用のインストルメントツールを使用する

  1. [製品]->[プロファイル]をクリックします
  2. リークをクリックします
  3. リンクを確認してください
于 2012-10-20T11:39:46.323 に答える