English 中文(简体)
mutilThread ios
原标题:mutilThread ios

我的申请功能是把手工艺从两个单人之间转移。

The user can choose lots of pic to send. But since the send process need time, so right now I just put the pics that have not yet been sent into an array when the user picks it from the photo-library.

我现在的问题是,如果用户在一个时间选择太多的剂量,我的申请就会突然终止。 现在,我的解决办法是,如果用户在某一时间选择太多的剂量,那么我就只能把标记放在某些手法上,而把他们放在光图书馆里,同时把他们放在阵列中。 然后,我又在发送行动同时从图书馆装上手工艺品。 因此,我在此指出,我怀疑,如此多的论题被放在一阵列中,是申请终止的原因,因此,我想把一些论题放在磁盘上,然后把其他部分放在磁盘上,我的代码是:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 100)
    {
        if (buttonIndex == 1)
        {
            inArray = YES;
            NSLog(@"---------%d",[self.imageArr count]);
            if ([self.imageArr count] < kCachedImages) {
                [self.imageArr addObject:thisImageObj];
                [thisImageObj release];
            }
            else
            [self.imageURLArr addObject:photoURL];

            if(!isSending)
            {
                currentPos = -1;
                [self sendImage];
            }
            [sndTableView reloadData];
        }
        else if (buttonIndex == 2)
        {
            if (inArray) {
                if ([self.imageArr count] < kCachedImages) {
                    [self.imageArr addObject:thisImageObj];
                    [thisImageObj release];
                }else {
                    [self.imageURLArr addObject:photoURL];
                }
            }else {
                [self.imageArr addObject:thisImageObj];
            }

           if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
               [self.popOverView dismissPopoverAnimated:YES];
            }
           else {
               [self dismissModalViewControllerAnimated:YES];
           }


            [sndTableView reloadData];
            if(!isSending)
            {
                currentPos = -1;
                [self sendImage];
            }
        }

    }

}


- (void)sendImage
{
    if(![self.imageArr count])
    {
        [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
        isSending = NO;
        [self.navigationItem setHidesBackButton:NO animated:YES];
        return; //end of all sending
    }


    NSMutableData *toBeSend = [NSMutableData dataWithCapacity:sizeof(packetHeader)+kBuffer];
    imageObj *imgObj;
    if ([self.imageArr count]) {
        imgObj = [[self.imageArr objectAtIndex:0]retain];
    }

  //here created another thread to load pics from library  
    if ([self.imageURLArr count] > 0) {
                         [NSThread detachNewThreadSelector:@selector(load) toTarget:self withObject:nil];
    }


    packetHeader header;
    header.magic = 0x5577ACAC;
    header.seq = currentSeq++;

    if(currentPos < 0) //first packet
    {
        UIImage *img = [UIImage imageWithData:imgObj.pimage];
        self.currentSendingImg.image = img;

        //thumbnail
        img = [TransferViewController imageWithImage:img scaledToSize:CGSizeMake(70, 70)];
        NSData *data = UIImageJPEGRepresentation(img, 0.5);

        header.command = 16;
        header.commandData1 = imgObj.imageSize;
        header.commandData2 = (imgObj.imageWidth<<16) + imgObj.imageLength;
        header.commandData3 = [data length];
//      NSLog(@"thumbnail data length = %u", header.commandData3);
        [toBeSend appendData:[NSData dataWithBytes:&header length:sizeof(packetHeader)]];
        [toBeSend appendData:data];
        currentPos = 0;
        isSending = YES;
        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
        [self.navigationItem setHidesBackButton:YES animated:YES];
    }
    else
    {
        if(currentPos + kBuffer < imgObj.imageSize)
        {
            header.command = 19;
            header.commandData1 = kBuffer;
            [toBeSend appendData:[NSData dataWithBytes:&header length:sizeof(packetHeader)]];
            NSRange r;
            r.location = currentPos;
            r.length = kBuffer;
            [toBeSend appendData:[imgObj.pimage subdataWithRange:r]];
            currentPos += kBuffer;
        }
        else
        {//last packet
            header.command = 17;
            header.commandData1 = imgObj.imageSize - currentPos;
            [toBeSend appendData:[NSData dataWithBytes:&header length:sizeof(packetHeader)]];
            NSRange r;
            r.location = currentPos;
            r.length = header.commandData1;
            [toBeSend appendData:[imgObj.pimage subdataWithRange:r]];
            currentPos = -1;
            if ([self.imageArr count]) {
                [self.imageArr removeObjectAtIndex:0];
            }
            [self.sndTableView reloadData];
        }
    }

    [manager sendPacket:toBeSend];
    self.sendingProgess.progress = (float)currentPos / (float)imgObj.imageSize;

    [imgObj release];
}


- (void)load 
{


    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    if (![imageURLArr count]) {
        return;
    }        
    int imageSum = [imageURLArr count];

        NSURL *path = [self.imageURLArr objectAtIndex:0] ;

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:path resultBlock:^(ALAsset *asset)
         {
             ALAssetRepresentation *representation = [asset defaultRepresentation];
             CGImageRef ref = [representation fullResolutionImage];
             if (ref != nil) {
                 omg = [UIImage imageWithCGImage:ref];
                 [omg retain];
             }

         }
                failureBlock:^(NSError *error)
         {
             // error handling
         }];
        [library release];

        if (omg) {
            NSData *data = UIImageJPEGRepresentation(omg, kcompressionQuality);
            thisImageObj = [imageObj alloc];
            thisImageObj.imageSize = [data length];
            thisImageObj.imageWidth = omg.size.width;
            thisImageObj.imageLength = omg.size.height;
            thisImageObj.pimage = data;
            [omg release];
        }
        else {
            return;
        }


    imageSum--;

    if ([self.imageURLArr count]) {
        [self.imageURLArr removeObjectAtIndex:0];
    }

    [self.imageArr addObject:thisImageObj];
    [thisImageObj release];

    [NSThread sleepForTimeInterval:1.0];

    [pool release];

}

但是,现在其他近距离工作的方法,以及已经结束的问题,人们可以就如何做到这一点向我提出一些建议,或者说,根本上需要另一条线。

最佳回答

你们首先在座有几个问题。 首先,请您使用<代码>detach NewThreadSelector:to Target:withObject:,这几乎永远不是正确的工具。 这可以产生数量无限制的深层。 这可能是你坠毁的,但这种做法非常危险。 更好的解决办法类似:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  [self load];
};

This will correctly manage your thread pool.

你们的主要问题是,你在没有任何锁定的情况下重新获得ars。 NSMutableArray is not thread safe. 你们只能对它作任何修改。 GCD也在这里提供帮助。 迈克 Ash则做了一个很好的写字,称为 您可使用<条码>发送_sync到读和<条码>发送_barrier_async书写。

尽管如此,你确实需要这种背景。

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签