What is the correct way to import & save Photos from iPhone Album?

后端 未结 2 1870
面向向阳花
面向向阳花 2020-12-24 04:32

I am Importing Photos from the iPhone Album to the documents folder of my application. This is my code.

for (int j=0; j<[assetArray count]; j++) {

    A         


        
2条回答
  •  醉酒成梦
    2020-12-24 04:49

    I was using ELCImagePicker and was facing same problem while importing multiple photos at a time from photos library using asselts. We can not reduce time taken to import but crash issue will be resolved.

    for (int j=0; j<[assetArray count]; j++) 
    {
        @autoreleasepool // This is compiler level feature so will only work on xcode 4.1 or above
        {
             ALAsset *assest = [assetArray objectAtIndex:j];
             CGImageRef imageRef = assest.defaultRepresentation.fullResolutionImage;
             UIImage *image = [UIImage imageWithCGImage:imageRef];
             NSData *imageData = UIImagePNGRepresentation(image);
             [imageData writeToFile:documentsPath atomically:YES];
        }
    }
    

    If possible try to store only AssetURL in assetArray instead of whole ALAsset object and create ALAsset at a time from url so may be it helps to reduce memory consumption. In such case you will need to use blocks as

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        CGImageRef iref = [[myasset defaultRepresentation] fullResolutionImage];
        if (iref) //You have image so use it 
        {
        }
    };
    
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can't get image - %@",[myerror localizedDescription]);
    };
    
    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:imageURL resultBlock:resultblock failureBlock:failureblock];
    

提交回复
热议问题