从iPhone上传图片的问题?

时间:2011-06-08 19:58:08

标签: iphone image upload

我正在尝试从iPhone上传图片。当我的资源中有图像时,我可以在苹果文档的帮助下轻松上传ftp图像http://developer.apple.com/library/ios/#samplecode/SimpleFTPSample/Introduction/Intro.html

现在我从我的图书馆获取图像,当上传图像时,它需要无限时间,甚至不会崩溃。好吧,我在这里分享我的代码

那么任何人都可以帮我了解如何上传该图片吗? 提前谢谢

这是从PhotoViewController.m中的库中获取图像的操作

- (IBAction)sendAction:(UIView *)sender
{
    assert( [sender isKindOfClass:[UIView class]] );

    if ( ! self.isSending ) {
        NSString *  filePath;
        int a;
        a = [currentUserID intValue];
        NSLog(@"%@   %i", currentUserID, a);
        filePath = [[iFeelAppDelegate sharedAppDelegate] pathForTestImage:a];
        assert(filePath != nil);

        [self _startSend:filePath];
    }
}

现在这段代码在我的appDelegate

- (NSString *)pathForTestImage:(NSUInteger)imageNumber
{
    NSUInteger          power;
    NSUInteger          expansionFactor;
    NSString *          originalFilePath;
    NSString *          bigFilePath;
    NSFileManager *     fileManager;
    NSDictionary *      attrs;
    unsigned long long  originalFileSize;
    unsigned long long  bigFileSize;
 power = imageNumber - 1;
#if TARGET_IPHONE_SIMULATOR
    power += 1;
#endif
    expansionFactor = (NSUInteger) pow(10, power);

    fileManager = [NSFileManager defaultManager];
    assert(fileManager != nil);

    // Calculate paths to both the original file and the expanded file.

 /*   originalFilePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Image%zu", (size_t) imageNumber] ofType:@"png"];

    assert(originalFilePath != nil);
   */ 
    bigFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%zu.png", (size_t) imageNumber]];
    assert(bigFilePath != nil);

    // Get the sizes of each.

  /*  attrs = [fileManager attributesOfItemAtPath:originalFilePath error:NULL];
    assert(attrs != nil);

    originalFileSize = [[attrs objectForKey:NSFileSize] unsignedLongLongValue];*/

    attrs = [fileManager attributesOfItemAtPath:bigFilePath error:NULL];
    if (attrs == NULL) {
        bigFileSize = 0;
    } else {
        bigFileSize = [[attrs objectForKey:NSFileSize] unsignedLongLongValue];
    }

    // If the expanded file is missing, or the wrong size, create it from scratch.

    if (bigFileSize != (originalFileSize * expansionFactor)) {
        NSOutputStream *    bigFileStream;
        NSData *            data;
        const uint8_t *     dataBuffer;
        NSUInteger          dataLength;
        NSUInteger          dataOffset;
        NSUInteger          counter;

        NSLog(@"%5u - %@", (size_t) expansionFactor, bigFilePath);

//        data = [NSData dataWithContentsOfMappedFile:originalFilePath];
        data = [NSData dataWithContentsOfMappedFile:bigFilePath];
        assert(data != nil);

        dataBuffer = [data bytes];
        dataLength = [data length];

        bigFileStream = [NSOutputStream outputStreamToFileAtPath:bigFilePath append:NO];
        assert(bigFileStream != NULL);

        [bigFileStream open];

        for (counter = 0; counter < expansionFactor; counter++) {
            dataOffset = 0;
            while (dataOffset != dataLength) {
                NSInteger       bytesWritten;

                bytesWritten = [bigFileStream write:&dataBuffer[dataOffset] maxLength:dataLength - dataOffset];
                assert(bytesWritten > 0);

                dataOffset += bytesWritten;
            }
        }

        [bigFileStream close];
    }

    return bigFilePath;
}

- (NSString *)pathForTemporaryFileWithPrefix:(NSString *)prefix
{
    NSString *  result;
    CFUUIDRef   uuid;
    CFStringRef uuidStr;

    uuid = CFUUIDCreate(NULL);
    assert(uuid != NULL);

    uuidStr = CFUUIDCreateString(NULL, uuid);
    assert(uuidStr != NULL);

    result = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@", prefix, uuidStr]];
    assert(result != nil);

    CFRelease(uuidStr);
    CFRelease(uuid);

    return result;
}

- (NSURL *)smartURLForString:(NSString *)str
{
    NSURL *     result;
    NSString *  trimmedStr;
    NSRange     schemeMarkerRange;
    NSString *  scheme;

    assert(str != nil);

    result = nil;

    trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
        schemeMarkerRange = [trimmedStr rangeOfString:@"://"];

        if (schemeMarkerRange.location == NSNotFound) {
            result = [NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@", trimmedStr]];
        } else {
            scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
            assert(scheme != nil);

            if ( ([scheme compare:@"ftp"  options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
                result = [NSURL URLWithString:trimmedStr];
            } else {
                // It looks like this is some unsupported URL scheme.
            }
        }
    }

    return result;
}


- (BOOL)isImageURL:(NSURL *)url
{
    BOOL        result;
    NSString *  path;
    NSString *  extension;

    assert(url != nil);

    path = [url path];
    result = NO;
    if (path != nil) {
        extension = [path pathExtension];
        if (extension != nil) {
            result = ([extension caseInsensitiveCompare:@"gif"] == NSOrderedSame)
            || ([extension caseInsensitiveCompare:@"png"] == NSOrderedSame)
            || ([extension caseInsensitiveCompare:@"jpg"] == NSOrderedSame);
        }
    }
    return result;
}

- (void)didStartNetworking
{
    self.networkingCount += 1;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)didStopNetworking
{
  //  assert(self.networkingCount > 0);
    if (self.networkingCount <= 0) {
            self.networkingCount -= 1;
    }


    [UIApplication sharedApplication].networkActivityIndicatorVisible = (self.networkingCount != 0);
}

0 个答案:

没有答案