проблема с загрузкой изображения с iPhone? - PullRequest
1 голос
/ 08 июня 2011

Я пытаюсь загрузить изображение с iPhone. когда у меня есть изображение в моих ресурсах, тогда я могу легко загрузить изображение на FTP с помощью документации Apple 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];
    }
}

Теперь этот код находится в моем приложении.

- (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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...