2016-03-25 22 views
5

Belirtilen dosyayı (fotoğraf makinesinin rulosundan gelen fotoğrafı) zaman uyumsuz olarak okumak istiyorum ancak benim için çalışmaz.PHAsset içeriği eşzamansız olarak okunamıyor

Değişken tempDatanil e kadar ben YES için yapılandırma requestOptionForPhotos.synchronous değiştirmek alır, sonra her şey tamam, ama bu kod senkron gerçekleştirmek istemiyoruz.

Diğer dosyalarda aynı dosya için istekte bulunarak fotoğrafa erişimi engelleyebiliyor muyum? Ben objektif-c ve iOS programlamasında yeniyim ve nasıl çalıştığını bilmiyorum.

NSURL *assetUrl = [[NSURL alloc] initWithString:filepath]; 
    PHFetchResult *collection = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:assetUrl] options:nil]; 

    PHImageRequestOptions *requestOptionForPhotos = [[PHImageRequestOptions alloc] init]; 
    requestOptionForPhotos.networkAccessAllowed = YES; 
    requestOptionForPhotos.synchronous = NO; 

    __block BOOL isFinished = NO; 
    __block NSData * tempData = nil; 

    for(PHAsset *asset in collection) { 
    [[PHImageManager defaultManager] 
    requestImageForAsset:asset 
    targetSize:CGSizeMake(80, 80) 
    contentMode:PHImageContentModeAspectFill 
    options:requestOptionForPhotos 
    resultHandler:^(UIImage *result, NSDictionary *info) { 
     tempData = UIImagePNGRepresentation(result); 
     isFinished = YES; 
    }]; 
    } 
+0

Geçerli bir dosya yolu alıyor musunuz? Ayrıca, "koleksiyon" nesnesinde aldığınız herhangi bir veri var mı? –

+0

Evet, bazı veriler ve yollar da geçerli. – siwymilek

cevap

0
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); 
    dispatch_async(queue, ^{ 
     NSURL *assetUrl = [[NSURL alloc] initWithString:filepath]; 
     PHFetchResult *collection = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:assetUrl] options:nil]; 
     PHImageRequestOptions *requestOptionForPhotos = [[PHImageRequestOptions alloc] init]; 
     requestOptionForPhotos.networkAccessAllowed = YES; 
     requestOptionForPhotos.synchronous = NO; 

     __block BOOL isFinished = NO; 
     __block NSData * tempData = nil; 

     for (PHAsset *asset in collection) { 
      [[PHImageManager defaultManager] 
         requestImageForAsset:asset 
         targetSize:CGSizeMake(80, 80) 
         contentMode:PHImageContentModeAspectFill 
         options:requestOptionForPhotos 
         resultHandler:^(UIImage *result, NSDictionary *info) { 
          tempData = UIImagePNGRepresentation(result); 
          isFinished = YES; 
      }]; 
     } 
    }); 

asynchronusly görüntüyü almak ve görüntü alma veya olmayan hava durumunu kontrol etmek sonuç handdler üzerinde kesme noktası koymak için bu kodu deneyin.

İlgili konular