0

ファイルの配列を .csv ファイルに保存できましたNSDocumentDirectory。これが私が使用したものです:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
         ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
        //NSData *imageData = UIImagePNGRepresentation(image);
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

私の問題は、この形式の画像を@"Images%d.png"このコードにロードする方法です:

.h

@property (strong, nonatomic) NSMutableArray *images;

.m

    self.images = ??????
4

3 に答える 3

1

に画像を保存するためのNSDocumentDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];//m_nImageIndex
        NSString *imagename = [NSString stringWithFormat:@"savedImage%d.png",m_nImageIndex];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imagename];
        // Get PNG data from following method
        NSData *myData =     UIImagePNGRepresentation(image);
        // It is better to get JPEG data because jpeg data will store the location and other related information of image.
        [myData writeToFile:savedImagePath atomically:NO];
        //[UIImageJPEGRepresentation(image ,1) writeToFile:savedImagePath atomically:NO];

画像取得用

          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"savedImage%d.png",indexPath.row]];
          UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

          cell.backgroundView = [[UIImageView alloc] initWithImage:img];
于 2013-09-26T10:35:34.427 に答える
0

バンドルから画像名を読み取るためのコードは次のとおりです。バンドルパスをドキュメントディレクトリパスに変更してください。コードスニペットは、ソートされた画像名の配列を提供し、名前を読み取るためにforループを使用する手間を省きます。

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *unsortedImages = [dirContents filteredArrayUsingPredicate:filter];
NSArray *sortedImages = [unsortedImages 
                     sortedArrayUsingComparator:
                     ^NSComparisonResult (id obj1, id obj2){
                         return [(NSString*)obj1 compare:(NSString*)obj2 
                                                 options:NSNumericSearch];
                    }];

フレンドリーなアドバイス:アプリが拒否される可能性があるため、ダウンロード可能なデータ/画像をNSDocumentDirectoryディレクトリに保存しないでください。代わりにNSCachesDirectoryを使用してください。この投稿とAppleの開発者ポータルの関連ドキュメントをお読みください。

乾杯!!
アマール。

于 2012-05-29T06:35:13.050 に答える
0

png 拡張子を jpg ファイルとして保存しています。私はこれがうまくいくと信じています:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.jpg", i]];
    ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

    // to load the image later:
    UIImage *imageFromDisk = [UIImage imageWithContentsOfFile:savedImagePath];
于 2012-05-30T16:48:47.887 に答える