识别IOS相册中图片的格式

#pragma 判断文件格式
-(NSString *)typeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
            
        case 0xFF:
            
            return @"image/jpeg";
            
        case 0x89:
            
            return @"image/png";
            
        case 0x47:
            
            return @"image/gif";
            
        case 0x49:
            
        case 0x4D:
            
            return @"image/tiff";
            
    }    
    return nil;
    
}
#pragma 图片选择模块
 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
         self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
        if(takephoto==false)
    {
        NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL];
       
        ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];
        void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset) {
        
        if (asset != nil) {
            
          //  [[NSFileManager defaultManager] removeItemAtPath:[PaiKeUnity getPrevImgPath] error:nil];
            
            ALAssetRepresentation *rep = [asset defaultRepresentation];
            Byte *imageBuffer = (Byte*)malloc(rep.size);
            NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];
            NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
            NSString *str= [self typeForImageData:imageData];
            if([str isEqual:@"image/png"])
                png=true;
            else
                png=false;
            
        }
        else {
        }
      };
    
       [assetLibrary assetForURL:imageRefURL
                     resultBlock:ALAssetsLibraryAssetForURLResultBlock
                    failureBlock:^(NSError *error){
                    }];
        
    }
        if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
        {
            
                 chosenImage=[info objectForKey:UIImagePickerControllerOriginalImage];
                 _contentimageview.image=[self fixOrientation:chosenImage];
            
        }
    
      if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
            {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
                    [alert show];
           
            }
    
       [picker dismissViewControllerAnimated:YES completion:nil];
  }

你可能感兴趣的:(识别IOS相册中图片的格式)